public void Publish() { var id = nameof(MessengerTest); var msg = "ClientToServer"; var actual = string.Empty; using (var server = new Messenger <string>(id)) using (var client = new MessengerClient <string>(id)) { var cts = new CancellationTokenSource(); Action <string> h = (x) => { actual = x; server.Publish(x); cts.Cancel(); }; IDisposable uns = null; try { uns = server.Subscribe(h); Task.Run(() => client.Publish(msg)).Forget(); Task.Delay(TimeSpan.FromSeconds(5), cts.Token).Wait(); } catch (AggregateException /* err */) { uns.Dispose(); } } Assert.That(actual, Is.EqualTo(msg)); }