Skip to content

Commit 6656950

Browse files
committed
CSHARP-2064: Change stream examples for docs.
1 parent 985e8e6 commit 6656950

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright 2017 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Threading;
18+
using FluentAssertions;
19+
using MongoDB.Bson;
20+
using MongoDB.Driver.Tests;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Examples
24+
{
25+
public class ChangeStreamExamples
26+
{
27+
[Fact]
28+
public void ChangeStreamExample1()
29+
{
30+
var client = DriverTestConfiguration.Client;
31+
var database = client.GetDatabase("ChangeStreamExamples");
32+
database.DropCollection("inventory");
33+
var inventory = database.GetCollection<BsonDocument>("inventory");
34+
35+
var document = new BsonDocument("x", 1);
36+
new Thread(() =>
37+
{
38+
Thread.Sleep(TimeSpan.FromMilliseconds(100));
39+
inventory.InsertOne(document);
40+
})
41+
.Start();
42+
43+
// Start Changestream Example 1
44+
var enumerator = inventory.Watch().ToEnumerable().GetEnumerator();
45+
enumerator.MoveNext();
46+
var next = enumerator.Current;
47+
enumerator.Dispose();
48+
// End Changestream Example 1
49+
50+
next.FullDocument.Should().Be(document);
51+
}
52+
53+
[Fact]
54+
public void ChangeStreamExample2()
55+
{
56+
var client = DriverTestConfiguration.Client;
57+
var database = client.GetDatabase("ChangeStreamExamples");
58+
database.DropCollection("inventory");
59+
var inventory = database.GetCollection<BsonDocument>("inventory");
60+
61+
var document = new BsonDocument("x", 1);
62+
inventory.InsertOne(document);
63+
new Thread(() =>
64+
{
65+
Thread.Sleep(TimeSpan.FromMilliseconds(100));
66+
var filter = new BsonDocument("_id", document["_id"]);
67+
var update = "{ $set : { x : 2 } }";
68+
inventory.UpdateOne(filter, update);
69+
})
70+
.Start();
71+
72+
// Start Changestream Example 2
73+
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
74+
var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator();
75+
enumerator.MoveNext();
76+
var next = enumerator.Current;
77+
enumerator.Dispose();
78+
// End Changestream Example 2
79+
80+
var expectedFullDocument = document.Set("x", 2);
81+
next.FullDocument.Should().Be(expectedFullDocument);
82+
}
83+
84+
[Fact]
85+
public void ChangeStreamExample3()
86+
{
87+
var client = DriverTestConfiguration.Client;
88+
var database = client.GetDatabase("ChangeStreamExamples");
89+
database.DropCollection("inventory");
90+
var inventory = database.GetCollection<BsonDocument>("inventory");
91+
92+
var documents = new[]
93+
{
94+
new BsonDocument("x", 1),
95+
new BsonDocument("x", 2)
96+
};
97+
98+
ChangeStreamDocument<BsonDocument> lastChangeStreamDocument;
99+
{
100+
new Thread(() =>
101+
{
102+
Thread.Sleep(TimeSpan.FromMilliseconds(100));
103+
inventory.InsertMany(documents);
104+
})
105+
.Start();
106+
107+
var enumerator = inventory.Watch().ToEnumerable().GetEnumerator();
108+
enumerator.MoveNext();
109+
lastChangeStreamDocument = enumerator.Current;
110+
}
111+
112+
{
113+
// Start Changestream Example 3
114+
var resumeToken = lastChangeStreamDocument.ResumeToken;
115+
var options = new ChangeStreamOptions { ResumeAfter = resumeToken };
116+
var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator();
117+
enumerator.MoveNext();
118+
var next = enumerator.Current;
119+
enumerator.Dispose();
120+
// End Changestream Example 3
121+
122+
next.FullDocument.Should().Be(documents[1]);
123+
}
124+
}
125+
}
126+
}

tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
</ItemGroup>
6565
<ItemGroup>
6666
<Compile Include="AggregatePrimer.cs" />
67+
<Compile Include="ChangeStreamExamples.cs" />
6768
<Compile Include="DocumentationExamples.cs" />
6869
<Compile Include="IndexesPrimer.cs" />
6970
<Compile Include="InsertPrimer.cs" />

0 commit comments

Comments
 (0)