-
Notifications
You must be signed in to change notification settings - Fork 24
/
RfqStateMachineTests.cs
120 lines (108 loc) · 4.15 KB
/
RfqStateMachineTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reflection;
using NUnit.Framework;
using RfqStateMachine;
using Stateless;
using Tests.TestDoubles;
namespace Tests
{
[TestFixture]
public class RfqStateMachineTests
{
private RfqServiceDouble _rfqServiceDouble;
private ConcurrencyServiceDouble _concurrencyServiceDouble;
private IRfq _sut;
private List<RfqUpdate> _updates;
private Exception _error;
private bool _completed;
[SetUp]
public void SetUp()
{
_rfqServiceDouble = new RfqServiceDouble();
_concurrencyServiceDouble = new ConcurrencyServiceDouble();
_sut = new Rfq(_rfqServiceDouble, _concurrencyServiceDouble);
_updates = new List<RfqUpdate>();
_error = null;
_completed = false;
_sut.Updates.Subscribe(
_updates.Add,
ex => _error = ex,
() => _completed = true);
}
[Test]
public void HappyPathScenario()
{
// user request quote
_sut.RequestQuote(null);
// server sends quote
_rfqServiceDouble.RequestQuoteSubject.OnNext(null);
// user executes
_sut.Execute(null);
// server sends execution report
_rfqServiceDouble.ExecuteSubject.OnNext(null);
Assert.AreEqual(5, _updates.Count);
Assert.AreEqual(RfqState.Input, _updates[0].RfqState);
Assert.AreEqual(RfqState.Requesting, _updates[1].RfqState);
Assert.AreEqual(RfqState.Quoted, _updates[2].RfqState);
Assert.AreEqual(RfqState.Executing, _updates[3].RfqState);
Assert.AreEqual(RfqState.Done, _updates[4].RfqState);
}
[Test]
public void MultipleServerQuotes()
{
// user request quote
_sut.RequestQuote(null);
// server sends quotes
_rfqServiceDouble.RequestQuoteSubject.OnNext(null);
_rfqServiceDouble.RequestQuoteSubject.OnNext(null);
Assert.AreEqual(4, _updates.Count);
Assert.AreEqual(RfqState.Input, _updates[0].RfqState);
Assert.AreEqual(RfqState.Requesting, _updates[1].RfqState);
Assert.AreEqual(RfqState.Quoted, _updates[2].RfqState);
Assert.AreEqual(RfqState.Quoted, _updates[3].RfqState);
}
[Test]
public void UserCancelRequest()
{
// user request quote
_sut.RequestQuote(null);
// user cancels
_sut.Cancel(32);
// server acks
_rfqServiceDouble.CancelSubject.OnNext(Unit.Default);
Assert.AreEqual(4, _updates.Count);
Assert.AreEqual(RfqState.Input, _updates[0].RfqState);
Assert.AreEqual(RfqState.Requesting, _updates[1].RfqState);
Assert.AreEqual(RfqState.Cancelling, _updates[2].RfqState);
Assert.AreEqual(RfqState.Cancelled, _updates[3].RfqState);
}
[Test]
public void UserCancelQuote()
{
// user request quote
_sut.RequestQuote(null);
// server sends quotes
_rfqServiceDouble.RequestQuoteSubject.OnNext(null);
// user cancels
_sut.Cancel(32);
// server acks
_rfqServiceDouble.CancelSubject.OnNext(Unit.Default);
Assert.AreEqual(5, _updates.Count);
Assert.AreEqual(RfqState.Input, _updates[0].RfqState);
Assert.AreEqual(RfqState.Requesting, _updates[1].RfqState);
Assert.AreEqual(RfqState.Quoted, _updates[2].RfqState);
Assert.AreEqual(RfqState.Cancelling, _updates[3].RfqState);
Assert.AreEqual(RfqState.Cancelled, _updates[4].RfqState);
}
[Test]
public void GenerateStateDiagram()
{
var field = _sut.GetType().GetField("_stateMachine", BindingFlags.Instance | BindingFlags.NonPublic);
var stateMachine = (StateMachine<RfqState, RfqEvent>) field.GetValue(_sut);
Console.WriteLine(stateMachine.ToStateDiagram());
}
}
}