public async Task SendCommand_SingleCommand_ShouldWriteOnResponseStream()
        {
            // Arrange
            var message = new Command()
            {
                Id       = EnvelopeId.NewId(),
                Method   = CommandMethod.Set,
                Uri      = "/greeting",
                Resource = "Hello world"
            };
            var target = GetTarget();

            // Act
            await target.SendCommandAsync(message, CancellationTokenSource.Token);

            // Assert
            ResponseBody.Position.ShouldBePositive();
            ResponseBody.Position = 0;
            using var reader      = new StreamReader(ResponseBody, Encoding.UTF8);
            var json = await reader.ReadToEndAsync();

            var actualEnvelope = EnvelopeSerializer.Deserialize(json);
            var actualCommand  = actualEnvelope.ShouldBeOfType <Command>();

            actualCommand.Id.ShouldBe(message.Id);
            actualCommand.Method.ShouldBe(message.Method);
            actualCommand.Uri.ShouldBe(message.Uri);
            actualCommand.Type.ShouldBe(message.Type);
            actualCommand.Resource.ToString().ShouldBe(message.Resource.ToString());
        }
        public async Task SendHugeEnvelope()
        {
            try
            {
                var sw = Stopwatch.StartNew();

                var serializer = new EnvelopeSerializer(new DocumentTypeResolver().WithMessagingDocuments());

                var path     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
                var content  = File.ReadAllLines(Path.Combine(path, "huge-json.txt"));
                var envelope = serializer.Deserialize(string.Join("", content));

                await _clientTransport.SendAsync(envelope, _cancellationToken);

                await _serverTransport.ReceiveAsync(_cancellationToken);

                sw.Stop();

                // Assert
                sw.ElapsedMilliseconds.ShouldBeLessThan(100);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
            }
        }
        public async Task SendMessage_SingleMessage_ShouldWriteOnResponseStream()
        {
            // Arrange
            var message = new Message()
            {
                Id      = EnvelopeId.NewId(),
                Content = "Hello world"
            };
            var target = GetTarget();

            // Act
            await target.SendMessageAsync(message, CancellationTokenSource.Token);

            // Assert
            ResponseBody.Position.ShouldBePositive();
            ResponseBody.Position = 0;
            using var reader      = new StreamReader(ResponseBody, Encoding.UTF8);
            var json = await reader.ReadToEndAsync();

            var actualEnvelope = EnvelopeSerializer.Deserialize(json);
            var actualMessage  = actualEnvelope.ShouldBeOfType <Message>();

            actualMessage.Id.ShouldBe(message.Id);
            actualMessage.Type.ShouldBe(message.Type);
            actualMessage.Content.ToString().ShouldBe(message.Content.ToString());
        }
        public async Task SendNotification_SingleNotification_ShouldWriteOnResponseStream()
        {
            // Arrange
            var notification = new Notification()
            {
                Id    = EnvelopeId.NewId(),
                Event = Event.Received
            };
            var target = GetTarget();

            // Act
            await target.SendNotificationAsync(notification, CancellationTokenSource.Token);

            // Assert
            ResponseBody.Position.ShouldBePositive();
            ResponseBody.Position = 0;
            using var reader      = new StreamReader(ResponseBody, Encoding.UTF8);
            var json = await reader.ReadToEndAsync();

            var actualEnvelope     = EnvelopeSerializer.Deserialize(json);
            var actualNotification = actualEnvelope.ShouldBeOfType <Notification>();

            actualNotification.Id.ShouldBe(notification.Id);
            actualNotification.Event.ShouldBe(notification.Event);
        }
        private void assertRoundTrips(int index)
        {
            var contentType = messageSerializers[index].ContentType;
            var envelope    = new Envelope()
            {
                Message     = theAddress,
                ContentType = contentType
            };

            theSerializer.Serialize(envelope, new ChannelNode());

            envelope.Message = theSerializer.Deserialize(envelope);

            envelope.Message.ShouldNotBeTheSameAs(theAddress);
            envelope.Message.ShouldBe(theAddress);
        }
Beispiel #6
0
        // POST api/values
        public void Post([FromBody] JObject jsonObject)
        {
            var envelopeSerializer = new EnvelopeSerializer();

            var notification = envelopeSerializer.Deserialize(jsonObject.ToString());

            Console.WriteLine("Received Notification");
        }
        /// <inheritdoc />
        /// <exception cref="InvalidOperationException">Thrown when the transport is not connected.</exception>
        public override async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            ThrowIfNotConnected();

            var envelopeSerialized = await EnvelopeChannel.Reader.ReadAsync(cancellationToken);

            await TraceWriter.TraceIfEnabledAsync(envelopeSerialized, DataOperation.Receive).ConfigureAwait(false);

            return(EnvelopeSerializer.Deserialize(envelopeSerialized));
        }
        public async Task Send100HugeEnvelopesAsync()
        {
            // Arrange
            var count = 100;

            var serializer   = new EnvelopeSerializer(new DocumentTypeResolver().WithMessagingDocuments());
            var path         = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
            var content      = File.ReadAllLines(Path.Combine(path, "huge.json"));
            var hugeEnvelope = (Command)serializer.Deserialize(string.Join("", content));

            var envelopes = Enumerable
                            .Range(0, count)
                            .Select(i =>
            {
                var command = hugeEnvelope.ShallowCopy();
                command.Id  = $"envelope_{i}";
                return(command);
            });

            var receivedEnvelopes = Enumerable
                                    .Range(0, count)
                                    .Select(i => _serverTransport.ReceiveAsync(_cancellationToken))
                                    .ToArray();

            // Act
            var sw = Stopwatch.StartNew();

            foreach (var envelope in envelopes)
            {
                await _clientTransport.SendAsync(envelope, _cancellationToken);
            }

            await Task.WhenAll(receivedEnvelopes);

            sw.Stop();

            // Assert
            sw.ElapsedMilliseconds.ShouldBeLessThan(count * 100);
        }