public async Task TestBasicFeature()
        {
            var sw = new Stopwatch();

            sw.Start();
            var trafficReader = new TcpDataReader(1024);

            var incomingArray = new byte[] { 1, 2, 3, 4, 5, 6 };

            var task = trafficReader.NewPackageAsync(incomingArray);

            var token = new CancellationTokenSource();
            var data  = await trafficReader.ReadAsyncAsync(3, token.Token);

            TestExtensions.AsReadOnlyMemory(incomingArray, 0, 3).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            data = trafficReader.ReadAsyncAsync(2, token.Token).Result;
            TestExtensions.AsReadOnlyMemory(incomingArray, 3, 2).ArrayIsEqualWith(data.AsArray());
            trafficReader.CommitReadData(data);

            await task;

            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
        public async Task TestInitTableContract()
        {
            var serializer = new MyNoSqlTcpSerializer();


            var testContract = new InitTableContract
            {
                TableName = "Test",
                Data      = new byte[] { 1, 2, 3 }
            };



            var dataReader = new TcpDataReader(ReadBufferSize);

            var rawData = serializer.Serialize(testContract);
            await dataReader.NewPackageAsync(rawData);

            var result
                = (InitTableContract)await serializer
                  .DeserializeAsync(dataReader, CancellationToken.None);

            Assert.AreEqual(testContract.TableName, result.TableName);
            Assert.AreEqual(testContract.Data.Length, result.Data.Length);
            for (var i = 0; i < testContract.Data.Length; i++)
            {
                Assert.AreEqual(testContract.Data[i], result.Data[i]);
            }
        }
Esempio n. 3
0
        public async Task TestByte()
        {
            var data = new byte[] { 1, 2, 3, 4, 5 };

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(data);

            var tc     = new CancellationTokenSource();
            var result = await tcpDataReader.ReadAndCommitByteAsync(tc.Token);

            Assert.AreEqual(1, result);
        }
        public async Task TestFindingTheSequenceFeatureAtTheSameArray()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 3, 4, 5, 13 };

            await trafficReader.NewPackageAsync(incomingArray1);

            var tc   = new CancellationTokenSource();
            var data = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, tc.Token);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3, 4, 5 }).ArraysAreEqual(data.AsArray());
        }
        public async Task TestFindingTheSequenceFeatureAtTheSameArray()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray1 = new byte[] { 1, 2, 15, 4, 5, 13 };

            await trafficReader.NewPackageAsync(incomingArray1);

            var token = new CancellationTokenSource();
            var data  = await trafficReader.ReadWhileWeGetSequenceAsync(new byte[] { 4, 5 }, token.Token);

            Console.WriteLine("DataLen:" + data.Length);

            new ReadOnlyMemory <byte>(new byte[] { 1, 2, 15, 4, 5 }).ArrayIsEqualWith(data.AsArray());
        }
Esempio n. 6
0
        public async Task TestInt()
        {
            var       data      = new byte[10];
            const int testValue = 1234567;

            BitConverter.TryWriteBytes(data, testValue);

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(data);

            var tc     = new CancellationTokenSource();
            var result = await tcpDataReader.ReadIntAsync(tc.Token);

            Assert.AreEqual(testValue, result);
        }
Esempio n. 7
0
        public async Task TestByteArray()
        {
            var memoryStream = new MemoryStream();
            var testValue    = new byte[] { 1, 2, 3, 4, 5, 6 };

            memoryStream.WriteByteArray(testValue);

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(memoryStream.ToArray());

            var tc     = new CancellationTokenSource();
            var result = await tcpDataReader.ReadByteArrayAsync(tc.Token);

            TestExtensions.AsReadOnlyMemory(testValue).ArraysAreEqual(result);
        }
Esempio n. 8
0
        public async Task TestString()
        {
            var          memoryStream = new MemoryStream();
            const string testValue    = "My test String";

            memoryStream.WriteString(testValue);

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(memoryStream.ToArray());

            var tc     = new CancellationTokenSource();
            var result = await tcpDataReader.ReadStringAsync(tc.Token);

            Assert.AreEqual(testValue, result);
        }
        public async Task TestShort()
        {
            var         data      = new byte[10];
            const short testValue = 30000;

            BitConverter.TryWriteBytes(data, testValue);

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(data);

            var token  = new CancellationTokenSource();
            var result = await tcpDataReader.ReadShortAsync(token.Token);

            Assert.AreEqual(testValue, result);
        }
        public async Task TestULong()
        {
            var         data      = new byte[10];
            const ulong testValue = 123456789012;

            BitConverter.TryWriteBytes(data, testValue);

            var tcpDataReader = new TcpDataReader(1024);

            await tcpDataReader.NewPackageAsync(data);

            var token  = new CancellationTokenSource();
            var result = await tcpDataReader.ReadULongAsync(token.Token);

            Assert.AreEqual(testValue, result);
        }
        public async Task TestBasicFeature()
        {
            var trafficReader = new TcpDataReader(1024);

            var incomingArray = new byte[] { 1, 2, 3, 4, 5, 6 };

            await trafficReader.NewPackageAsync(incomingArray);

            var tc   = new CancellationTokenSource();
            var data = await trafficReader.ReadAsyncAsync(3, tc.Token);

            TestExtensions.AsReadOnlyMemory(incomingArray, 0, 3).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);

            data = await trafficReader.ReadAsyncAsync(2, tc.Token);

            TestExtensions.AsReadOnlyMemory(incomingArray, 3, 2).ArraysAreEqual(data.AsArray());
            trafficReader.CommitReadData(data);
        }
        public async Task TestPong()
        {
            var serializer = new MyNoSqlTcpSerializer();


            var testContract = new PongContract();

            var dataReader = new TcpDataReader(ReadBufferSize);

            var rawData = serializer.Serialize(testContract);

            await dataReader.NewPackageAsync(rawData);

            var tc = new CancellationTokenSource();

            var result
                = await serializer
                  .DeserializeAsync(dataReader, tc.Token);

            Assert.IsTrue(typeof(PongContract) == result.GetType());
        }
        public async Task TestSubscribeContract()
        {
            var serializer = new MyNoSqlTcpSerializer();

            var testContract = new SubscribeContract
            {
                TableName = "Test"
            };


            var dataReader = new TcpDataReader(ReadBufferSize);

            var rawData = serializer.Serialize(testContract);
            await dataReader.NewPackageAsync(rawData);

            var result
                = (SubscribeContract)await serializer
                  .DeserializeAsync(dataReader, CancellationToken.None);

            Assert.AreEqual(testContract.TableName, result.TableName);
        }
        public async Task TestReadyAmountWayBiggerThenAmountOfBuffer()
        {
            var dataReader = new TcpDataReader(64);

            var list = new List <byte>();

            for (byte i = 0; i < 255; i++)
            {
                list.Add(i);
            }

            var src       = list.ToArray();
            var writeTask = dataReader.NewPackageAsync(src);


            var result = await dataReader.ReadAsyncAsync(255, new CancellationTokenSource().Token);

            src.ArraysAreEqual(result.AsArray());
            dataReader.CommitReadData(result);

            await writeTask;
        }