Permalink
Browse files

QuitHandler - QuitSendsCancellation

  • Loading branch information...
bobbymcr committed Dec 5, 2018
1 parent 35acc2f commit 212445970f296bb0903a1e06350885a750a3a618
Showing with 65 additions and 0 deletions.
  1. +36 −0 core/src/QuitHandler.cs
  2. +29 −0 core/test/QuitHandlerTest.cs
@@ -0,0 +1,36 @@
// <copyright file="QuitHandler.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
using System;
using System.Threading;

public sealed class QuitHandler : IDisposable
{
private readonly CancellationTokenSource cts;

public QuitHandler(MessageBus bus, string verb)
{
this.cts = new CancellationTokenSource();
bus.Subscribe<SentenceMessage>(m => this.Handle(m.Verb.Primary == verb));
}

public CancellationToken Token => this.cts.Token;

public void Dispose()
{
}

private bool Handle(bool shouldQuit)
{
if (shouldQuit)
{
this.cts.Cancel();
}

return shouldQuit;
}
}
}
@@ -0,0 +1,29 @@
// <copyright file="QuitHandlerTest.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure.Test
{
using System;
using System.Collections.Generic;
using System.Threading;
using FluentAssertions;
using Xunit;

public class QuitHandlerTest
{
[Fact]
public void QuitSendsCancellation()
{
MessageBus bus = new MessageBus();
using (QuitHandler handler = new QuitHandler(bus, "scram"))
{
handler.Token.IsCancellationRequested.Should().BeFalse();

bus.Send(new SentenceMessage(new Word("scram", "quit"), new Word(string.Empty, string.Empty)));

handler.Token.IsCancellationRequested.Should().BeTrue();
}
}
}
}

0 comments on commit 2124459

Please sign in to comment.