@@ -26,41 +26,41 @@ public function setUp()
26
26
*/
27
27
public function writeLockEmptyCollection ()
28
28
{
29
- $ staleTimestamp = new \MongoDate (time () + 1000 );
30
-
31
- $ this ->locker ->writeLock ('theId ' , $ staleTimestamp );
29
+ $ this ->locker ->writeLock ('theId ' , 1000 );
32
30
33
31
$ this ->assertSame (1 , $ this ->collection ->count ());
34
- $ expected = [
35
- '_id ' => 'theId ' ,
36
- 'writing ' => true ,
37
- 'writeStaleTs ' => $ staleTimestamp ,
38
- 'writePending ' => false ,
39
- 'readers ' => [],
40
- ];
32
+
41
33
$ actual = $ this ->collection ->findOne ();
34
+
35
+ $ actualWriteStaleTs = $ actual ['writeStaleTs ' ]->sec ;
36
+ unset($ actual ['writeStaleTs ' ]);
37
+
38
+ $ expected = ['_id ' => 'theId ' , 'readers ' => [], 'writePending ' => false , 'writing ' => true ];
42
39
ksort ($ actual );
43
- $ this ->assertEquals ($ expected , $ actual );
40
+ $ this ->assertSame ($ expected , $ actual );
41
+
42
+ $ this ->assertGreaterThanOrEqual (time () + 1000 , $ actualWriteStaleTs );
43
+ $ this ->assertLessThan (time () + 1010 , $ actualWriteStaleTs );
44
44
}
45
45
46
46
/**
47
47
* @test
48
48
*/
49
49
public function writeLockClearStuckWrite ()
50
50
{
51
- $ this ->locker ->writeLock ('theId ' , new \ MongoDate () );
51
+ $ this ->locker ->writeLock ('theId ' , 0 );
52
52
53
- $ this ->locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
53
+ $ this ->locker ->writeLock ('theId ' , 1000 );
54
54
}
55
55
56
56
/**
57
57
* @test
58
58
*/
59
59
public function writeLockClearStuckRead ()
60
60
{
61
- $ this ->locker ->readLock ('theId ' , new \ MongoDate () );
61
+ $ this ->locker ->readLock ('theId ' , 0 );
62
62
63
- $ this ->locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
63
+ $ this ->locker ->writeLock ('theId ' , 1000 );
64
64
}
65
65
66
66
/**
@@ -72,40 +72,66 @@ public function writeLockTimeout()
72
72
{
73
73
$ locker = new Locker ($ this ->collection , 100000 , 1 );
74
74
75
- $ locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
76
- $ locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
75
+ $ locker ->writeLock ('theId ' , 1000 );
76
+ $ locker ->writeLock ('theId ' , 1000 );
77
77
}
78
78
79
79
/**
80
80
* @test
81
+ * @expectedException \InvalidArgumentException
82
+ * @expectedExceptionMessage $staleDuration must be an int >= 0
81
83
*/
82
- public function readLockEmptyCollection ()
84
+ public function writeLockNonIntStaleDuration ()
85
+ {
86
+ (new Locker ($ this ->collection ))->writeLock ('theId ' , true );
87
+ }
88
+
89
+ /**
90
+ * @test
91
+ * @expectedException \InvalidArgumentException
92
+ * @expectedExceptionMessage $staleDuration must be an int >= 0
93
+ */
94
+ public function writeLockNegativeStaleDuration ()
83
95
{
84
- $ staleTimestamp = new \MongoDate (time () + 1000 );
96
+ (new Locker ($ this ->collection ))->writeLock ('theId ' , -1 );
97
+ }
85
98
86
- $ readerId = $ this ->locker ->readLock ('theId ' , $ staleTimestamp );
99
+ /**
100
+ * @test
101
+ */
102
+ public function readLockEmptyCollection ()
103
+ {
104
+ $ readerId = $ this ->locker ->readLock ('theId ' , 1000 );
87
105
88
106
$ this ->assertSame (1 , $ this ->collection ->count ());
89
- $ expected = [
90
- '_id ' => 'theId ' ,
91
- 'writing ' => false ,
92
- 'writePending ' => false ,
93
- 'readers ' => [['id ' => $ readerId , 'staleTs ' => $ staleTimestamp ]],
94
- 'writeStaleTs ' => null ,
95
- ];
107
+
96
108
$ actual = $ this ->collection ->findOne ();
109
+
110
+ $ actualReaders = $ actual ['readers ' ];
111
+ unset($ actual ['readers ' ]);
112
+
113
+ $ expected = ['_id ' => 'theId ' , 'writePending ' => false , 'writeStaleTs ' => null , 'writing ' => false ];
114
+
97
115
ksort ($ actual );
98
- $ this ->assertEquals ($ expected , $ actual );
116
+ $ this ->assertSame ($ expected , $ actual );
117
+
118
+ $ this ->assertCount (1 , $ actualReaders );
119
+ $ this ->assertCount (2 , $ actualReaders [0 ]);
120
+
121
+ $ this ->assertInstanceOf ('\MongoId ' , $ actualReaders [0 ]['id ' ]);
122
+
123
+ $ this ->assertGreaterThanOrEqual (time () + 1000 , $ actualReaders [0 ]['staleTs ' ]->sec );
124
+ $ this ->assertLessThan (time () + 1010 , $ actualReaders [0 ]['staleTs ' ]->sec );
99
125
}
100
126
101
127
/**
102
128
* @test
103
129
*/
104
130
public function readLockClearStuck ()
105
131
{
106
- $ this ->locker ->writeLock ('theId ' , new \ MongoDate () );
132
+ $ this ->locker ->writeLock ('theId ' , 0 );
107
133
108
- $ this ->locker ->readLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
134
+ $ this ->locker ->readLock ('theId ' , 1000 );
109
135
}
110
136
111
137
/**
@@ -117,16 +143,36 @@ public function readLockTimeout()
117
143
{
118
144
$ locker = new Locker ($ this ->collection , 100000 , 1 );
119
145
120
- $ locker ->writeLock ('theId ' , new \MongoDate (time () + 1000 ));
121
- $ locker ->readLock ('theId ' , new \MongoDate (time () + 1000 ));
146
+ $ locker ->writeLock ('theId ' , 1000 );
147
+ $ locker ->readLock ('theId ' , 1000 );
148
+ }
149
+
150
+ /**
151
+ * @test
152
+ * @expectedException \InvalidArgumentException
153
+ * @expectedExceptionMessage $staleDuration must be an int >= 0
154
+ */
155
+ public function readLockNonIntStaleDuration ()
156
+ {
157
+ (new Locker ($ this ->collection ))->readLock ('theId ' , true );
158
+ }
159
+
160
+ /**
161
+ * @test
162
+ * @expectedException \InvalidArgumentException
163
+ * @expectedExceptionMessage $staleDuration must be an int >= 0
164
+ */
165
+ public function readLockNegativeStaleDuration ()
166
+ {
167
+ (new Locker ($ this ->collection ))->readLock ('theId ' , -1 );
122
168
}
123
169
124
170
/**
125
171
* @test
126
172
*/
127
173
public function writeUnlock ()
128
174
{
129
- $ this ->locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
175
+ $ this ->locker ->writeLock ('theId ' , 1000 );
130
176
$ this ->locker ->writeUnlock ('theId ' );
131
177
132
178
$ this ->assertSame (0 , $ this ->collection ->count ());
@@ -137,7 +183,7 @@ public function writeUnlock()
137
183
*/
138
184
public function readUnlockEmptyCollection ()
139
185
{
140
- $ readerId = $ this ->locker ->readLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
186
+ $ readerId = $ this ->locker ->readLock ('theId ' , 1000 );
141
187
$ this ->locker ->readUnlock ('theId ' , $ readerId );
142
188
143
189
$ this ->assertSame (0 , $ this ->collection ->count ());
@@ -148,23 +194,30 @@ public function readUnlockEmptyCollection()
148
194
*/
149
195
public function readUnlockExistingReader ()
150
196
{
151
- $ existingStaleTimestamp = new \MongoDate (time () + 1000 );
152
- $ existingReaderId = $ this ->locker ->readLock ('theId ' , $ existingStaleTimestamp );
197
+ $ existingReaderId = $ this ->locker ->readLock ('theId ' , 1000 );
153
198
154
- $ readerId = $ this ->locker ->readLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
199
+ $ readerId = $ this ->locker ->readLock ('theId ' , 1000 );
155
200
$ this ->locker ->readUnlock ('theId ' , $ readerId );
156
201
157
202
$ this ->assertSame (1 , $ this ->collection ->count ());
158
- $ expected = [
159
- '_id ' => 'theId ' ,
160
- 'writing ' => false ,
161
- 'writePending ' => false ,
162
- 'readers ' => [['id ' => $ existingReaderId , 'staleTs ' => $ existingStaleTimestamp ]],
163
- 'writeStaleTs ' => null ,
164
- ];
203
+
165
204
$ actual = $ this ->collection ->findOne ();
205
+
206
+ $ actualReaders = $ actual ['readers ' ];
207
+ unset($ actual ['readers ' ]);
208
+
209
+ $ expected = ['_id ' => 'theId ' , 'writePending ' => false , 'writeStaleTs ' => null , 'writing ' => false ];
210
+
166
211
ksort ($ actual );
167
- $ this ->assertEquals ($ expected , $ actual );
212
+ $ this ->assertSame ($ expected , $ actual );
213
+
214
+ $ this ->assertCount (1 , $ actualReaders );
215
+ $ this ->assertCount (2 , $ actualReaders [0 ]);
216
+
217
+ $ this ->assertInstanceOf ('\MongoId ' , $ actualReaders [0 ]['id ' ]);
218
+
219
+ $ this ->assertGreaterThanOrEqual (time () + 1000 , $ actualReaders [0 ]['staleTs ' ]->sec );
220
+ $ this ->assertLessThan (time () + 1010 , $ actualReaders [0 ]['staleTs ' ]->sec );
168
221
}
169
222
170
223
/**
@@ -178,7 +231,7 @@ public function twoWriters()
178
231
$ locker = new Locker ($ db ->selectCollection ('locks ' ), 0 );
179
232
180
233
for ($ i = 0 ; $ i < 500 ; ++$ i ) {
181
- $ locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
234
+ $ locker ->writeLock ('theId ' , 1000 );
182
235
183
236
$ dataCollection ->update (['_id ' => 1 ], ['_id ' => 1 , 'key ' => $ keyOne ], ['upsert ' => true ]);
184
237
$ dataCollection ->update (['_id ' => 2 ], ['_id ' => 2 , 'key ' => $ keyTwo ], ['upsert ' => true ]);
@@ -219,7 +272,7 @@ public function oneWriterOneReader()
219
272
$ locker = new Locker ($ db ->selectCollection ('locks ' ), 0 );
220
273
221
274
while (true ) {
222
- $ readerId = $ locker ->readLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
275
+ $ readerId = $ locker ->readLock ('theId ' , 1000 );
223
276
224
277
$ docs = iterator_to_array ($ dataCollection ->find ([], ['_id ' => 0 ])->sort (['_id ' => 1 ]), false );
225
278
if ($ docs !== [] && $ docs !== [['key ' => 1 ], ['key ' => 2 ], ['key ' => 3 ]]) {
@@ -236,7 +289,7 @@ public function oneWriterOneReader()
236
289
$ locker = new Locker ($ db ->selectCollection ('locks ' ), 0 );
237
290
238
291
for ($ i = 0 ; $ i < 1000 ; ++$ i ) {
239
- $ locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
292
+ $ locker ->writeLock ('theId ' , 1000 );
240
293
241
294
$ dataCollection ->update (['_id ' => 1 ], ['_id ' => 1 , 'key ' => 1 ], ['upsert ' => true ]);
242
295
$ dataCollection ->update (['_id ' => 2 ], ['_id ' => 2 , 'key ' => 2 ], ['upsert ' => true ]);
@@ -271,7 +324,7 @@ public function twoWritersTwoReaders()
271
324
$ locker = new Locker ($ db ->selectCollection ('locks ' ), 0 );
272
325
273
326
while (true ) {
274
- $ readerId = $ locker ->readLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
327
+ $ readerId = $ locker ->readLock ('theId ' , 1000 );
275
328
276
329
$ docs = iterator_to_array ($ dataCollection ->find ([], ['_id ' => 0 ])->sort (['_id ' => 1 ]), false );
277
330
if ($ docs !== [] &&
@@ -290,7 +343,7 @@ public function twoWritersTwoReaders()
290
343
$ locker = new Locker ($ db ->selectCollection ('locks ' ), 0 );
291
344
292
345
for ($ i = 0 ; $ i < 200 ; ++$ i ) {
293
- $ locker ->writeLock ('theId ' , new \ MongoDate ( time () + 1000 ) );
346
+ $ locker ->writeLock ('theId ' , 1000 );
294
347
295
348
$ dataCollection ->update (['_id ' => 1 ], ['_id ' => 1 , 'key ' => $ keyOne ], ['upsert ' => true ]);
296
349
$ dataCollection ->update (['_id ' => 2 ], ['_id ' => 2 , 'key ' => $ keyTwo ], ['upsert ' => true ]);
0 commit comments