TestQueue.cs 1.9 KB
Newer Older
麦壳饼's avatar
麦壳饼 已提交
1 2 3 4 5 6 7 8 9 10 11 12
using IoTSharp.Queue;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace IoTSharp.Test
{
    [TestClass]
    public class TestQueue
    {
麦壳饼's avatar
麦壳饼 已提交
13
        Dictionary<string, object> dic = new Dictionary<string, object>();
麦壳饼's avatar
麦壳饼 已提交
14 15 16 17 18 19 20 21 22
        [TestInitialize]
        public void InitTest()
        {
            for (int ix = 0; ix < 10; ix++)
            {
                dic.Add($"key_{ix}", GenerateCheckCodeNum(1000));
            }
        }

麦壳饼's avatar
麦壳饼 已提交
23

24 25

    
麦壳饼's avatar
麦壳饼 已提交
26

麦壳饼's avatar
麦壳饼 已提交
27 28 29 30 31 32 33 34
        [TestMethod]
        public void TestSimpleQueue() => TestIMsgQueue<SimpleQueue>();

        [TestMethod]
        public void TestFastQueue() => TestIMsgQueue<FastQueue>();

        [TestMethod]
        public void TestDiskQueue() => TestIMsgQueue<LiteDBQueue>();
35
    
麦壳饼's avatar
麦壳饼 已提交
36 37 38 39 40 41 42 43 44

        [TestMethod]
        public void TestMemoryQueue() => TestIMsgQueue<MemoryQueue>();

        public void TestIMsgQueue<T>() where T : IMsgQueue, new()
        {
            var t = new T();
            for (int i = 0; i < 10000; i++)
            {
麦壳饼's avatar
麦壳饼 已提交
45
                t.Enqueue(new RawMsg() { DataCatalog = Data.DataCatalog.TelemetryData, DataSide = Data.DataSide.AnySide, DeviceId = Guid.NewGuid(), MsgBody = dic, MsgType = MsgType.MQTT });
麦壳饼's avatar
麦壳饼 已提交
46 47 48
            }
            for (int i = 0; i < 10000; i++)
            {
麦壳饼's avatar
麦壳饼 已提交
49
                t.Dequeue();
麦壳饼's avatar
麦壳饼 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
            }
        }
        private int rep = 0;
        private string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }
    }
}