Skip to content
Permalink
Browse files

EndOfGame - EndOfGameSendsCancellation

  • Loading branch information...
bobbymcr committed Dec 22, 2018
1 parent 69d9d99 commit c9c1e05c280b29cf650895fca97987e8b2a0ce6e
Showing with 80 additions and 0 deletions.
  1. +34 −0 core/src/EndOfGame.cs
  2. +16 −0 core/src/Messages/EndOfGameMessage.cs
  3. +30 −0 core/test/EndOfGameTest.cs
@@ -0,0 +1,34 @@
// <copyright file="EndOfGame.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
using System;
using System.Threading;
using Adventure.Messages;

public sealed class EndOfGame : IDisposable
{
private readonly CancellationTokenSource cts;
private readonly IDisposable sub;

public EndOfGame(MessageBus bus)
{
this.cts = new CancellationTokenSource();
this.sub = bus.Subscribe<EndOfGameMessage>(m => this.OnEnd(bus, m.Text));
}

public CancellationToken Token => this.cts.Token;

public void Dispose()
{
}

private void OnEnd(MessageBus bus, string text)
{
bus.Output(text);
this.cts.Cancel();
}
}
}
@@ -0,0 +1,16 @@
// <copyright file="EndOfGameMessage.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure.Messages
{
public sealed class EndOfGameMessage
{
public EndOfGameMessage(string text)
{
this.Text = text;
}

public string Text { get; }
}
}
@@ -0,0 +1,30 @@
// <copyright file="EndOfGameTest.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure.Test
{
using Adventure.Messages;
using FluentAssertions;
using Xunit;

public class EndOfGameTest
{
[Fact]
public void EndOfGameSendsCancellation()
{
MessageBus bus = new MessageBus();
string lastOutput = null;
bus.Subscribe<OutputMessage>(m => lastOutput = m.Text);
using (EndOfGame end = new EndOfGame(bus))
{
end.Token.IsCancellationRequested.Should().BeFalse();

bus.Send(new EndOfGameMessage("It's over."));

end.Token.IsCancellationRequested.Should().BeTrue();
lastOutput.Should().Be("It's over.");
}
}
}
}

0 comments on commit c9c1e05

Please sign in to comment.
You can’t perform that action at this time.