1
+ using System ;
2
+ using Xunit ;
3
+ using System . Linq ;
4
+ using Microsoft . Azure . Commands . Common . Authentication . Models ;
5
+ using Microsoft . Azure . Commands . Common . Authentication ;
6
+
7
+ namespace Microsoft . Azure . Authentication . Test . UnitTests
8
+ {
9
+ class MockClock : IClock
10
+ {
11
+ public DateTime fakeNow { get ; set ; }
12
+ public bool IsDue ( DateTime lastCheckTime , TimeSpan freq )
13
+ {
14
+ return fakeNow - lastCheckTime >= freq ;
15
+ }
16
+ public void AddSecond ( int sec )
17
+ {
18
+ fakeNow = fakeNow . AddSeconds ( sec ) ;
19
+ }
20
+ }
21
+
22
+ public class FrequencyServiceTests
23
+ {
24
+ private FrequencyService _frequencyService ;
25
+
26
+ public FrequencyServiceTests ( )
27
+ {
28
+ _frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new Clock ( ) ) ;
29
+ }
30
+
31
+ [ Fact ]
32
+ public void TestAdd ( )
33
+ {
34
+ // Arrange
35
+ string featureName = "TestFeature" ;
36
+ TimeSpan frequency = new TimeSpan ( 0 , 0 , 5 ) ; // 5 seconds
37
+
38
+ // Act
39
+ _frequencyService . Register ( featureName , frequency ) ;
40
+
41
+ // Assert
42
+ var frequencyInfo = new FrequencyService . FrequencyInfo ( frequency , DateTime . Now ) ;
43
+ Assert . NotNull ( frequencyInfo ) ;
44
+ Assert . Equal ( frequency , frequencyInfo . Frequency ) ;
45
+ }
46
+
47
+ [ Fact ]
48
+ public void TestAddSession ( )
49
+ {
50
+ string featureName = "testFeature" ;
51
+ _frequencyService . RegisterInSession ( featureName ) ;
52
+
53
+ Assert . True ( _frequencyService . SessionLogic . ContainsKey ( featureName ) ) ;
54
+ Assert . False ( _frequencyService . SessionLogic [ featureName ] ) ;
55
+ }
56
+
57
+ [ Fact ]
58
+ public void TestCheck_SessionLogic ( )
59
+ {
60
+ string featureName = "testFeature" ;
61
+ _frequencyService . RegisterInSession ( featureName ) ;
62
+
63
+ bool businessCheck = true ;
64
+ bool businessCalled = false ;
65
+ _frequencyService . TryRun ( featureName , ( ) => businessCheck , ( ) => businessCalled = true ) ;
66
+
67
+ Assert . True ( businessCalled ) ;
68
+ Assert . True ( _frequencyService . SessionLogic [ featureName ] ) ;
69
+ }
70
+
71
+ [ Fact ]
72
+ public void TestCheck_Frequencies ( )
73
+ {
74
+ string featureName = "testFeature" ;
75
+ TimeSpan frequency = new TimeSpan ( 0 , 0 , 1 ) ;
76
+ _frequencyService . Register ( featureName , frequency ) ;
77
+
78
+ bool businessCheck = true ;
79
+ bool businessCalled = false ;
80
+ _frequencyService . TryRun ( featureName , ( ) => businessCheck , ( ) => businessCalled = true ) ;
81
+
82
+ Assert . True ( businessCalled ) ;
83
+ }
84
+
85
+ [ Fact ]
86
+ public void AddsFeatureToFrequencyService ( )
87
+ {
88
+ // Arrange
89
+ var featureName = "MyFeature" ;
90
+ var frequency = TimeSpan . FromMinutes ( 5 ) ;
91
+
92
+ // Act
93
+ _frequencyService . Register ( featureName , frequency ) ;
94
+
95
+ // Assert
96
+ Assert . NotNull ( _frequencyService ) ;
97
+ Assert . NotNull ( _frequencyService . GetAllFeatureNames ( ) ) ;
98
+ Assert . Contains ( featureName , _frequencyService . GetAllFeatureNames ( ) ) ;
99
+ }
100
+
101
+ [ Fact ]
102
+ public void DoesNotAddDuplicateToFrequencyService ( )
103
+ {
104
+ // Arrange
105
+ var featureName = "MyFeature1" ;
106
+ var frequency = TimeSpan . FromMinutes ( 5 ) ;
107
+
108
+ // Act
109
+ _frequencyService . Register ( featureName , frequency ) ;
110
+ _frequencyService . Register ( featureName , frequency ) ;
111
+
112
+ // Assert
113
+ Assert . Equal ( 1 , _frequencyService . GetAllFeatureNames ( ) . Count ( n => n == featureName ) ) ;
114
+ }
115
+
116
+ [ Fact ]
117
+ public void AddsSessionFeatureToFrequencyService ( )
118
+ {
119
+ // Arrange
120
+ var featureName = "MySessionFeature" ;
121
+
122
+ // Act
123
+ _frequencyService . RegisterInSession ( featureName ) ;
124
+
125
+ // Assert
126
+ Assert . Contains ( featureName , _frequencyService . GetAllFeatureNames ( ) ) ;
127
+ }
128
+
129
+ [ Fact ]
130
+ public void DoesNotAddDuplicateSessionToFrequencyService ( )
131
+ {
132
+ // Arrange
133
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new Clock ( ) ) ;
134
+ var featureName = "MySessionFeature" ;
135
+
136
+ // Act
137
+ frequencyService . RegisterInSession ( featureName ) ;
138
+ frequencyService . RegisterInSession ( featureName ) ;
139
+
140
+ // Assert
141
+ Assert . Equal ( 1 , frequencyService . GetAllFeatureNames ( ) . Count ( n => n == featureName ) ) ;
142
+ }
143
+
144
+ [ Fact ]
145
+ public void Check_FrequencyMet_ExecuteBusinessLogic ( )
146
+ {
147
+ // Arrange
148
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new Clock ( ) ) ;
149
+ var featureName = "MyFeature2" ;
150
+ var frequency = TimeSpan . FromMinutes ( 5 ) ;
151
+ var businessLogicExecuted = false ;
152
+ frequencyService . Register ( featureName , frequency ) ;
153
+
154
+ // Act
155
+ frequencyService . TryRun ( featureName , ( ) => true , ( ) => businessLogicExecuted = true ) ;
156
+
157
+ // Assert
158
+ Assert . True ( businessLogicExecuted ) ;
159
+ }
160
+
161
+ [ Fact ]
162
+ public void Check_FrequencyNotMet_DoesNotExecuteBusinessLogic ( )
163
+ {
164
+ // Arrange
165
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) ) ;
166
+ var featureName = "MyFeature3" ;
167
+ var frequency = TimeSpan . FromMinutes ( 5 ) ;
168
+ var businessLogicExecuted = false ;
169
+ frequencyService . Register ( featureName , frequency ) ;
170
+
171
+ // Act
172
+ frequencyService . TryRun ( featureName , ( ) => false , ( ) => businessLogicExecuted = true ) ;
173
+
174
+ // Assert
175
+ Assert . False ( businessLogicExecuted ) ;
176
+ }
177
+
178
+ [ Fact ]
179
+ public void Check_SessionFeatureFirstTime_ExecuteBusinessLogic ( )
180
+ {
181
+ // Arrange
182
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new Clock ( ) ) ;
183
+ var featureName = "MySessionFeature" ;
184
+ var businessLogicExecuted = false ;
185
+ frequencyService . RegisterInSession ( featureName ) ;
186
+
187
+ // Act
188
+ frequencyService . TryRun ( featureName , ( ) => true , ( ) => businessLogicExecuted = true ) ;
189
+
190
+ // Assert
191
+ Assert . True ( businessLogicExecuted ) ;
192
+ }
193
+
194
+ [ Fact ]
195
+ public void Check_SessionFeatureSecondTime_DoesNotExecuteBusinessLogic ( )
196
+ {
197
+ // Arrange
198
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new Clock ( ) ) ;
199
+ var featureName = "MySessionFeature" ;
200
+ var businessLogicExecuted = false ;
201
+ frequencyService . RegisterInSession ( featureName ) ;
202
+
203
+ // Act
204
+ frequencyService . TryRun ( featureName , ( ) => true , ( ) => businessLogicExecuted = true ) ;
205
+ frequencyService . TryRun ( featureName , ( ) => true , ( ) => businessLogicExecuted = false ) ;
206
+
207
+ // Assert
208
+ Assert . True ( businessLogicExecuted ) ;
209
+ }
210
+
211
+ [ Fact ]
212
+ public void Check_Frequency_Logic ( )
213
+ {
214
+ var frequencyService = new FrequencyService ( new MemoryDataStore ( ) , new MockClock ( ) ) ;
215
+ var featureName = "MyFeature4" ;
216
+ var frequency = TimeSpan . FromSeconds ( 1 ) ;
217
+ int businessValue = 13 ;
218
+ frequencyService . Register ( featureName , frequency ) ;
219
+
220
+ ( ( MockClock ) frequencyService . _clock ) . fakeNow = DateTime . Now ;
221
+ frequencyService . Check ( featureName , ( ) => true , ( ) => businessValue = 100 , DateTime . Now ) ;
222
+ Assert . Equal ( 100 , businessValue ) ;
223
+
224
+ ( ( MockClock ) frequencyService . _clock ) . AddSecond ( 2 ) ;
225
+ frequencyService . Check ( featureName , ( ) => true , ( ) => businessValue = - 100 , ( ( MockClock ) frequencyService . _clock ) . fakeNow ) ;
226
+ Assert . Equal ( - 100 , businessValue ) ;
227
+
228
+ frequencyService . Check ( featureName , ( ) => true , ( ) => businessValue = 16 , ( ( MockClock ) frequencyService . _clock ) . fakeNow ) ;
229
+ Assert . Equal ( - 100 , businessValue ) ;
230
+
231
+
232
+ ( ( MockClock ) frequencyService . _clock ) . AddSecond ( 2 ) ;
233
+ frequencyService . Check ( featureName , ( ) => true , ( ) => businessValue = 17 , ( ( MockClock ) frequencyService . _clock ) . fakeNow ) ;
234
+ Assert . Equal ( 17 , businessValue ) ;
235
+ }
236
+ }
237
+ }
0 commit comments