public void SendBroadcast() { var branchA = new Yogi.Branch(context, "{\"name\":\"a\", \"_transceive_byte_limit\": 5}"); var branchB = new Yogi.Branch(context, "{\"name\":\"b\"}"); RunContextUntilBranchesAreConnected(context, branchA, branchB); context.RunInBackground(); // 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(); // Blocking for (int i = 0; i < 3; ++i) { Assert.True(branchA.SendBroadcast(bigJsonView, true)); } // Non-blocking while (branchA.SendBroadcast(bigJsonView, false)) { ; } context.Stop(); context.WaitForStopped(); // Verify that a broadcast has actually been sent while (!broadcastReceived) { context.RunOne(); } GC.KeepAlive(branchA); GC.KeepAlive(branchB); }
public void CancelReceiveBroadcast() { var branch = new Yogi.Branch(context, "{\"name\":\"a\"}"); Assert.False(branch.CancelReceiveBroadcast()); bool called = false; branch.ReceiveBroadcastAsync((res, _2, _3, _4) => { Assert.Equal(Yogi.ErrorCode.Canceled, res.ErrorCode); called = true; }); GC.Collect(); Assert.True(branch.CancelReceiveBroadcast()); context.Poll(); Assert.True(called); GC.KeepAlive(branch); }
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); }
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); }