-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRouting_T.java
More file actions
164 lines (125 loc) · 4.87 KB
/
Routing_T.java
File metadata and controls
164 lines (125 loc) · 4.87 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package unquietcode.tools.esm;
import org.junit.Assert;
import org.junit.Test;
import unquietcode.tools.esm.routing.RandomStateRouter;
import unquietcode.tools.esm.routing.RoundRobinStateRouter;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Ben Fagin
* @version 2013-04-02
*/
public class Routing_T {
@Test(expected=TransitionException.class)
public void testSyncWithinAsync() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<>(null);
esm.addAll(TestStates.class, true, true);
esm.onEntering(TestStates.Two, (state) -> {
esm.transition(TestStates.Three);
});
esm.transition(TestStates.One);
esm.transition(TestStates.Two);
Assert.fail();
}
@Test
public void testAsyncWithinAsync() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<>(null);
esm.addAll(TestStates.class, true, true);
final AtomicInteger c1 = new AtomicInteger(0);
final AtomicInteger c2 = new AtomicInteger(0);
final AtomicInteger c3 = new AtomicInteger(0);
esm.onEntering(TestStates.One, state -> c1.incrementAndGet());
esm.onEntering(TestStates.Two, (state) -> {
c2.incrementAndGet();
esm.transitionAsync(TestStates.Three);
});
esm.onEntering(TestStates.Three, state -> c3.incrementAndGet());
esm.transition(TestStates.One);
esm.transition(TestStates.Two);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertEquals(1, c1.get());
assertEquals(1, c2.get());
assertEquals(1, c3.get());
}
@Test
public void testSimpleRedirect() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<>(TestStates.One);
esm.addAll(TestStates.class, true);
final AtomicInteger c1 = new AtomicInteger(0);
final AtomicInteger c2 = new AtomicInteger(0);
final AtomicInteger c3 = new AtomicInteger(0);
esm.onEntering(TestStates.One, state -> c1.incrementAndGet());
esm.onEntering(TestStates.Two, state -> c2.incrementAndGet());
esm.onEntering(TestStates.Three, state -> c3.incrementAndGet());
esm.routeBeforeEntering(TestStates.Three, (current, next) -> {
assertEquals(next, TestStates.Three);
return TestStates.Two;
});
esm.transition(TestStates.One);
esm.transition(TestStates.Two);
esm.transition(TestStates.Three);
assertEquals(1, c1.get());
assertEquals(2, c2.get());
assertEquals(0, c3.get());
}
@Test
public void testRoundRobinRedirect() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<>(TestStates.One);
esm.addAll(TestStates.class, true);
final AtomicInteger c1 = new AtomicInteger(0);
final AtomicInteger c2 = new AtomicInteger(0);
final AtomicInteger c3 = new AtomicInteger(0);
esm.onEntering(TestStates.One, state -> c1.incrementAndGet());
esm.onEntering(TestStates.Two, state -> c2.incrementAndGet());
esm.onEntering(TestStates.Three, state -> c3.incrementAndGet());
RoundRobinStateRouter<TestStates> router = new RoundRobinStateRouter<>(TestStates.One, TestStates.Two, TestStates.Three);
esm.routeBeforeEntering(TestStates.Three, router);
for (int i=0; i < 12; ++i) {
esm.transition(TestStates.Three);
}
assertEquals(4, c1.get());
assertEquals(4, c2.get());
assertEquals(4, c3.get());
}
@Test
public void testRandomRedirect() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<>(TestStates.One);
esm.addAll(TestStates.class, true);
final AtomicInteger c1 = new AtomicInteger(0);
final AtomicInteger c2 = new AtomicInteger(0);
final AtomicInteger c3 = new AtomicInteger(0);
esm.onEntering(TestStates.One, state -> c1.incrementAndGet());
esm.onEntering(TestStates.Two, state -> c2.incrementAndGet());
esm.onEntering(TestStates.Three, state -> c3.incrementAndGet());
RandomStateRouter<TestStates> router = new RandomStateRouter<>(TestStates.One, TestStates.Two, TestStates.Three);
esm.routeBeforeEntering(TestStates.Three, router);
final int sampleCount = 1000;
// perform a bunch of transitions
for (int i=0; i < sampleCount; ++i) {
esm.transition(TestStates.Three);
}
// the sum should be the sample size
assertEquals(sampleCount, c1.get() + c2.get() + c3.get());
// the difference should less than 10%
final int epsilon = (int) (0.1 * sampleCount);
assertTrue(Math.abs(c1.get()-c2.get()) < epsilon);
assertTrue(Math.abs(c2.get()-c3.get()) < epsilon);
assertTrue(Math.abs(c3.get()-c1.get()) < epsilon);
}
@Test(expected=TransitionException.class)
public void testInvalidRoutingResult() {
EnumStateMachine<TestStates> esm = new EnumStateMachine<TestStates>();
esm.addTransition(null, TestStates.One);
esm.addTransition(TestStates.One, TestStates.Two);
// this should cause a failure, One -> Three not valid
esm.routeAfterExiting(TestStates.One, (current, next) -> TestStates.Three);
esm.transition(TestStates.One);
esm.transition(TestStates.Two);
}
enum TestStates { One, Two, Three }
}