-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support RingBuffer to get the latest sample. (#851)
* Add RingQueue to support getting the latest sample. Signed-off-by: ChenYing Kuo <[email protected]> * Rename RingQueue to RingBuffer. Signed-off-by: ChenYing Kuo <[email protected]> * Update examples. Signed-off-by: ChenYing Kuo <[email protected]> * Add document. Signed-off-by: ChenYing Kuo <[email protected]> * Add test for RingBuffer. Signed-off-by: ChenYing Kuo <[email protected]> * Use the correct naming convention (CameCase) Signed-off-by: ChenYing Kuo <[email protected]> * Add file header. Signed-off-by: ChenYing Kuo <[email protected]> * gename z_pull and update the usage. Signed-off-by: ChenYing Kuo <[email protected]> * Use ring instead of cache. Signed-off-by: ChenYing Kuo <[email protected]> * Add sleep to wait for the result in pubsub_with_ringbuffer. Signed-off-by: ChenYing Kuo <[email protected]> --------- Signed-off-by: ChenYing Kuo <[email protected]>
- Loading branch information
Showing
7 changed files
with
188 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
#[cfg(feature = "unstable")] | ||
#[test] | ||
fn pubsub() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
#[test] | ||
fn reuse() { | ||
zenoh::kedefine!( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
#[test] | ||
fn pubsub_with_ringbuffer() { | ||
use std::{thread, time::Duration}; | ||
use zenoh::{handlers::RingBuffer, prelude::sync::*}; | ||
|
||
let zenoh = zenoh::open(Config::default()).res().unwrap(); | ||
let sub = zenoh | ||
.declare_subscriber("test/ringbuffer") | ||
.with(RingBuffer::new(3)) | ||
.res() | ||
.unwrap(); | ||
for i in 0..10 { | ||
zenoh | ||
.put("test/ringbuffer", format!("put{i}")) | ||
.res() | ||
.unwrap(); | ||
} | ||
// Should only receive the last three samples ("put7", "put8", "put9") | ||
for i in 7..10 { | ||
assert_eq!( | ||
sub.recv() | ||
.unwrap() | ||
.unwrap() | ||
.payload() | ||
.deserialize::<String>() | ||
.unwrap(), | ||
format!("put{i}") | ||
); | ||
} | ||
// Wait for the subscriber to get the value | ||
thread::sleep(Duration::from_millis(1000)); | ||
} | ||
|
||
#[test] | ||
fn query_with_ringbuffer() { | ||
use zenoh::{handlers::RingBuffer, prelude::sync::*}; | ||
|
||
let zenoh = zenoh::open(Config::default()).res().unwrap(); | ||
let queryable = zenoh | ||
.declare_queryable("test/ringbuffer_query") | ||
.with(RingBuffer::new(1)) | ||
.res() | ||
.unwrap(); | ||
|
||
let _reply1 = zenoh | ||
.get("test/ringbuffer_query") | ||
.with_value("query1") | ||
.res() | ||
.unwrap(); | ||
let _reply2 = zenoh | ||
.get("test/ringbuffer_query") | ||
.with_value("query2") | ||
.res() | ||
.unwrap(); | ||
|
||
let query = queryable.recv().unwrap().unwrap(); | ||
// Only receive the latest query | ||
assert_eq!( | ||
query | ||
.value() | ||
.unwrap() | ||
.payload | ||
.deserialize::<String>() | ||
.unwrap(), | ||
"query2" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use std::sync::{Arc, Mutex}; | ||
use zenoh_core::zlock; | ||
|
||
|