Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
a1852b20
Commit
a1852b20
authored
Jun 21, 2019
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #621
parents
aac8e540
2c1cdb5f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
25 deletions
+100
-25
ChangeStream.php
src/ChangeStream.php
+36
-25
WatchFunctionalTest.php
tests/Operation/WatchFunctionalTest.php
+64
-0
No files found.
src/ChangeStream.php
View file @
a1852b20
...
...
@@ -102,22 +102,7 @@ class ChangeStream implements Iterator
{
try
{
$this
->
csIt
->
next
();
if
(
$this
->
valid
())
{
if
(
$this
->
hasAdvanced
)
{
$this
->
key
++
;
}
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
/* If the cursorId is 0, the server has invalidated the cursor so we
* will never perform another getMore. This means that we cannot
* resume and we can therefore unset the resumeCallable, which will
* free any reference to Watch. This will also free the only
* reference to an implicit session, since any such reference
* belongs to Watch. */
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
$this
->
onIteration
(
true
);
}
catch
(
RuntimeException
$e
)
{
if
(
$this
->
isResumableError
(
$e
))
{
$this
->
resume
();
...
...
@@ -133,14 +118,7 @@ class ChangeStream implements Iterator
{
try
{
$this
->
csIt
->
rewind
();
if
(
$this
->
valid
())
{
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
// As with next(), free the callable once we know it will never be used.
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
$this
->
onIteration
(
false
);
}
catch
(
RuntimeException
$e
)
{
if
(
$this
->
isResumableError
(
$e
))
{
$this
->
resume
();
...
...
@@ -160,7 +138,7 @@ class ChangeStream implements Iterator
/**
* Extracts the resume token (i.e. "_id" field) from the change document.
*
* @param array|
documen
t $document Change document
* @param array|
objec
t $document Change document
* @return mixed
* @throws InvalidArgumentException
* @throws ResumeTokenException if the resume token is not found or invalid
...
...
@@ -214,6 +192,38 @@ class ChangeStream implements Iterator
return
true
;
}
/**
* Perform housekeeping after an iteration event (i.e. next or rewind).
*
* @param boolean $isNext Whether the iteration event was a call to next()
* @throws ResumeTokenException
*/
private
function
onIteration
(
$isNext
)
{
/* If the cursorId is 0, the server has invalidated the cursor and we
* will never perform another getMore nor need to resume since any
* remaining results (up to and including the invalidate event) will
* have been received in the last response. Therefore, we can unset the
* resumeCallable. This will free any reference to Watch as well as the
* only reference to any implicit session created therein. */
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
if
(
!
$this
->
valid
())
{
return
;
}
/* Increment the key if the iteration event was a call to next() and we
* have already advanced past the first result. */
if
(
$isNext
&&
$this
->
hasAdvanced
)
{
$this
->
key
++
;
}
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
/**
* Creates a new changeStream after a resumable server error.
*
...
...
@@ -224,5 +234,6 @@ class ChangeStream implements Iterator
$newChangeStream
=
call_user_func
(
$this
->
resumeCallable
,
$this
->
resumeToken
);
$this
->
csIt
=
$newChangeStream
->
csIt
;
$this
->
csIt
->
rewind
();
$this
->
onIteration
(
false
);
}
}
tests/Operation/WatchFunctionalTest.php
View file @
a1852b20
...
...
@@ -322,6 +322,70 @@ class WatchFunctionalTest extends FunctionalTestCase
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
}
public
function
testResumeTokenIsUpdatedAfterResuming
()
{
$this
->
insertDocument
([
'_id'
=>
1
]);
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
$changeStream
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$changeStream
->
rewind
();
$this
->
assertNull
(
$changeStream
->
current
());
$this
->
insertDocument
([
'_id'
=>
2
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
2
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
2
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
$this
->
killChangeStreamCursor
(
$changeStream
);
$this
->
insertDocument
([
'_id'
=>
3
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
3
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
3
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
/* Triggering a consecutive failure will allow us to test whether the
* resume token was properly updated after the last resume. If the
* resume token updated, the next result will be {_id: 4}; otherwise,
* we'll see {_id: 3} returned again. */
$this
->
killChangeStreamCursor
(
$changeStream
);
$this
->
insertDocument
([
'_id'
=>
4
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
4
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
4
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
}
public
function
testKey
()
{
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment