Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUST-43 Add change streams examples for documentation #572

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/change_stream/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum FullDocumentType {
/// The
/// [`ChangeStreamEvent::full_document`](crate::change_stream::event::ChangeStreamEvent::full_document)
/// field will be populated with a copy of the entire document that was updated.
#[serde(rename = "updateLookup")]
UpdateLookup,

/// User-defined other types for forward compatibility.
Expand Down
79 changes: 77 additions & 2 deletions src/test/documentation_examples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
bson::{doc, Bson},
error::{ErrorKind, Result},
options::{ClientOptions, FindOptions, ServerApi, ServerApiVersion},
test::{TestClient, DEFAULT_URI, LOCK},
test::{log_uncaptured, TestClient, DEFAULT_URI, LOCK},
Client,
Collection,
};
Expand Down Expand Up @@ -1441,7 +1441,6 @@ async fn versioned_api_examples() -> GenericResult<()> {

use std::{error::Error, result::Result};

use crate::test::log_uncaptured;
// Start Versioned API Example 5
// With the `bson-chrono-0_4` feature enabled, this function can be dropped in favor of using
// `chrono::DateTime` values directly.
Expand Down Expand Up @@ -1755,6 +1754,81 @@ async fn index_examples() -> Result<()> {
Ok(())
}

async fn change_streams_examples() -> Result<()> {
use crate::{change_stream::options::FullDocumentType, options::ChangeStreamOptions, RUNTIME};
use std::time::Duration;

let client = TestClient::new().await;
if !client.is_replica_set() && !client.is_sharded() {
log_uncaptured("skipping change_streams_examples due to unsupported topology");
return Ok(());
}
let db = client.database("change_streams_examples");
db.drop(None).await?;
let inventory = db.collection::<Document>("inventory");
// Populate an item so the collection exists for the change stream to watch.
inventory.insert_one(doc! {}, None).await?;

// Background writer thread so that the `stream.next()` calls return something.
let (tx, mut rx) = tokio::sync::oneshot::channel();
let writer_inventory = inventory.clone();
let handle = RUNTIME
.spawn(async move {
let mut interval = RUNTIME.interval(Duration::from_millis(100));
loop {
tokio::select! {
_ = interval.tick() => {
writer_inventory.insert_one(doc! {}, None).await?;
}
_ = &mut rx => break,
}
}
Result::Ok(())
})
.unwrap();

#[allow(unused_variables, unused_imports)]
{
{
// Start Changestream Example 1
use futures::stream::TryStreamExt;
let mut stream = inventory.watch(None, None).await?;
let next = stream.try_next().await?;
// End Changestream Example 1
}

{
// Start Changestream Example 2
use futures::stream::TryStreamExt;
let options = ChangeStreamOptions::builder()
.full_document(Some(FullDocumentType::UpdateLookup))
.build();
let mut stream = inventory.watch(None, options).await?;
let next = stream.try_next().await?;
// End Changestream Example 2
}

{
let stream = inventory.watch(None, None).await?;
// Start Changestream Example 3
use futures::stream::TryStreamExt;
let resume_token = stream.resume_token();
let options = ChangeStreamOptions::builder()
.resume_after(resume_token)
.build();
let mut stream = inventory.watch(None, options).await?;
stream.try_next().await?;
// End Changestream Example 3
}
}

// Shut down the writer thread.
let _ = tx.send(());
handle.await?;

Ok(())
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn test() {
Expand All @@ -1780,4 +1854,5 @@ async fn test() {
aggregation_examples().await.unwrap();
run_command_examples().await.unwrap();
index_examples().await.unwrap();
change_streams_examples().await.unwrap();
}