Example #1
0
        public void CancelSendBroadcast()
        {
            var branchA = new Yogi.Branch(context,
                                          "{\"name\":\"a\", \"_transceive_byte_limit\": 5}");
            var branchB = new Yogi.Branch(context, "{\"name\":\"b\"}");

            RunContextUntilBranchesAreConnected(context, branchA, branchB);

            var oidToEc = new Dictionary <Yogi.OperationId, Yogi.ErrorCode>();

            while (true)
            {
                var oid = branchA.SendBroadcastAsync(bigJsonView, (res, opid) =>
                {
                    oidToEc[opid] = res.ErrorCode;
                });
                Assert.True(oid.IsValid);

                GC.Collect();

                oidToEc[oid] = Yogi.ErrorCode.Unknown;

                if (branchA.CancelSendBroadcast(oid))
                {
                    context.Poll();
                    Assert.Equal(Yogi.ErrorCode.Canceled, oidToEc[oid]);
                    break;
                }
            }

            GC.KeepAlive(branchA);
            GC.KeepAlive(branchB);
        }
Example #2
0
        public void ReceiveBroadcastAsync()
        {
            var branchA = new Yogi.Branch(context, "{\"name\":\"a\"}");
            var branchB = new Yogi.Branch(context, "{\"name\":\"b\"}");

            RunContextUntilBranchesAreConnected(context, branchA, branchB);

            // Simplest form
            var  uuidB  = branchB.Uuid;
            bool called = false;

            branchA.ReceiveBroadcastAsync((res, source, payload) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(uuidB, source);
                Assert.Equal(msgpackView, payload);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With buffer in handler function
            called = false;
            branchA.ReceiveBroadcastAsync((res, _, payload, buf) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(msgpackView, payload);
                Assert.True(buf.Length >= Yogi.Constants.MaxMessagePayloadSize);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With encoding
            called = false;
            branchA.ReceiveBroadcastAsync(Yogi.EncodingType.Json, (res, _, payload) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(jsonView, payload);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With encoding and with buffer in handler function
            called = false;
            branchA.ReceiveBroadcastAsync(Yogi.EncodingType.Json, (res, _, payload, buf) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(jsonView, payload);
                Assert.True(buf.Length >= Yogi.Constants.MaxMessagePayloadSize);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With custom buffer
            var buffer = new byte[123];

            called = false;
            branchA.ReceiveBroadcastAsync(buffer, (res, _, payload) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(msgpackView, payload);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With custom buffer and with buffer in handler function
            buffer = new byte[123];
            called = false;
            branchA.ReceiveBroadcastAsync(buffer, (res, _, payload, buf) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(msgpackView, payload);
                Assert.Equal(buffer, buf);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With custom buffer and encoding
            buffer = new byte[123];
            called = false;
            branchA.ReceiveBroadcastAsync(Yogi.EncodingType.Json, buffer, (res, _, payload) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(jsonView, payload);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            // With custom buffer and encoding and buffer in handler function
            buffer = new byte[123];
            called = false;
            branchA.ReceiveBroadcastAsync(Yogi.EncodingType.Json, buffer, (res, _, payload, buf) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(jsonView, payload);
                Assert.Equal(buffer, buf);
                called = true;
            });

            branchB.SendBroadcastAsync(msgpackView, (_1, _2) => { });
            GC.Collect();
            while (!called)
            {
                context.RunOne();
            }

            GC.KeepAlive(branchA);
            GC.KeepAlive(branchB);
        }
Example #3
0
        public void SendBroadcastAsync()
        {
            var branchA = new Yogi.Branch(context,
                                          "{\"name\":\"a\", \"_transceive_byte_limit\": 5}");
            var branchB = new Yogi.Branch(context, "{\"name\":\"b\"}");

            RunContextUntilBranchesAreConnected(context, branchA, branchB);

            // Receive a broadcast to verify that it has actually been sent
            bool broadcastReceived = false;

            branchB.ReceiveBroadcastAsync(Yogi.EncodingType.Json, (res, _, payload) =>
            {
                Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                Assert.Equal(bigJsonView, payload);
                broadcastReceived = true;
            });

            GC.Collect();

            // Send with retry = true
            int n       = 3;
            var results = new List <Yogi.Result>();

            for (int i = 0; i < n; ++i)
            {
                var oid = branchA.SendBroadcastAsync(bigJsonView, true, (res, opid) =>
                {
                    Assert.Equal(Yogi.ErrorCode.Ok, res.ErrorCode);
                    Assert.True(opid.IsValid);
                    results.Add(res);
                });
                Assert.True(oid.IsValid);
            }

            while (results.Count != n)
            {
                context.Poll();
            }

            // Send with retry = false
            do
            {
                branchA.SendBroadcastAsync(bigJsonView, false, (res, _) =>
                {
                    results.Add(res);
                });

                context.PollOne();
            } while (results[results.Count - 1].ErrorCode == Yogi.ErrorCode.Ok);

            Assert.Equal(Yogi.ErrorCode.TxQueueFull, results[results.Count - 1].ErrorCode);

            // Verify that a broadcast has actually been sent
            while (!broadcastReceived)
            {
                context.RunOne();
            }

            GC.KeepAlive(branchA);
            GC.KeepAlive(branchB);
        }