-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
Feature: Added unit test for watcher #1529
Open
ayushrakesh
wants to merge
2
commits into
kube-rs:main
Choose a base branch
from
ayushrakesh:test-watcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,16 +157,100 @@ | |
|
||
/// Used to control whether the watcher receives the full object, or only the | ||
/// metadata | ||
#[async_trait] | ||
trait ApiMode { | ||
type Value: Clone; | ||
// #[async_trait] | ||
// trait ApiMode { | ||
// type Value: Clone; | ||
|
||
// async fn list(&self, lp: &ListParams) -> kube_client::Result<ObjectList<Self::Value>>; | ||
// async fn watch( | ||
// &self, | ||
// wp: &WatchParams, | ||
// version: &str, | ||
// ) -> kube_client::Result<BoxStream<'static, kube_client::Result<WatchEvent<Self::Value>>>>; | ||
// } | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use async_trait::async_trait; | ||
use futures::stream::{self, BoxStream}; | ||
use kube_client::{Result, ListParams, WatchParams, ObjectList, WatchEvent}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Clone)] | ||
struct TestResource { | ||
// fields here | ||
} | ||
Comment on lines
+180
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we actually need a TestResource? Can we have our |
||
|
||
async fn list(&self, lp: &ListParams) -> kube_client::Result<ObjectList<Self::Value>>; | ||
async fn watch( | ||
&self, | ||
wp: &WatchParams, | ||
version: &str, | ||
) -> kube_client::Result<BoxStream<'static, kube_client::Result<WatchEvent<Self::Value>>>>; | ||
struct TestApiMode { | ||
list_response: Vec<ObjectList<TestResource>>, | ||
watch_response: Vec<Result<WatchEvent<TestResource>>>, | ||
list_call_count: Arc<Mutex<usize>>, | ||
watch_call_count: Arc<Mutex<usize>>, | ||
selectors: Arc<Mutex<Vec<ListParams>>>, | ||
} | ||
|
||
impl TestApiMode { | ||
fn new(list_response: Vec<ObjectList<TestResource>>, watch_response: Vec<Result<WatchEvent<TestResource>>>) -> Self { | ||
TestApiMode { | ||
list_response, | ||
watch_response, | ||
list_call_count: Arc::new(Mutex::new(0)), | ||
watch_call_count: Arc::new(Mutex::new(0)), | ||
selectors: Arc::new(Mutex::new(Vec::new())), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ApiMode for TestApiMode { | ||
type Value = TestResource; | ||
|
||
async fn list(&self, lp: &ListParams) -> Result<ObjectList<Self::Value>> { | ||
let mut count = self.list_call_count.lock().unwrap(); | ||
*count += 1; | ||
|
||
let mut selectors = self.selectors.lock().unwrap(); | ||
selectors.push(lp.clone()); | ||
|
||
Ok(self.list_response.get(*count - 1).cloned().unwrap_or_else(|| ObjectList::default())) | ||
} | ||
|
||
async fn watch(&self, wp: &WatchParams, _version: &str) -> Result<BoxStream<'static, Result<WatchEvent<Self::Value>>>> { | ||
let mut count = self.watch_call_count.lock().unwrap(); | ||
*count += 1; | ||
Ok(stream::iter(self.watch_response.clone()).boxed()) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_watcher_behavior() { | ||
// Simulate paginated list responses | ||
let list_response = vec![ | ||
ObjectList::<TestResource> { items: vec![TestResource { /* fields */ }], metadata: Default::default() }, | ||
ObjectList::<TestResource> { items: vec![TestResource { /* fields */ }], metadata: Default::default() }, | ||
]; | ||
let watch_response = vec![Ok(WatchEvent::Added(TestResource { /* fields */ }))]; | ||
|
||
let api_mode = TestApiMode::new(list_response, watch_response); | ||
|
||
// Create the watcher using the TestApiMode | ||
// Verify the watcher behavior with assertions | ||
|
||
// Verify list call count | ||
let list_call_count = api_mode.list_call_count.lock().unwrap(); | ||
assert_eq!(*list_call_count, 2); | ||
|
||
// Verify watch call count | ||
let watch_call_count = api_mode.watch_call_count.lock().unwrap(); | ||
assert_eq!(*watch_call_count, 1); | ||
|
||
// Verify selectors consistency | ||
let selectors = api_mode.selectors.lock().unwrap(); | ||
assert!(selectors.iter().all(|lp| lp.selector == expected_selector)); | ||
|
||
// Additional assertions for union of list and watch events and desync handling | ||
} | ||
} | ||
|
||
/// A wrapper around the `Api` of a `Resource` type that when used by the | ||
|
@@ -409,7 +493,7 @@ | |
} | ||
|
||
#[async_trait] | ||
impl<K> ApiMode for FullObject<'_, K> | ||
where | ||
K: Clone + Debug + DeserializeOwned + Send + 'static, | ||
{ | ||
|
@@ -435,7 +519,7 @@ | |
} | ||
|
||
#[async_trait] | ||
impl<K> ApiMode for MetaOnly<'_, K> | ||
where | ||
K: Clone + Debug + DeserializeOwned + Send + 'static, | ||
{ | ||
|
@@ -465,7 +549,7 @@ | |
state: State<A::Value>, | ||
) -> (Option<Result<Event<A::Value>>>, State<A::Value>) | ||
where | ||
A: ApiMode, | ||
A::Value: Resource + 'static, | ||
{ | ||
match state { | ||
|
@@ -668,7 +752,7 @@ | |
mut state: State<A::Value>, | ||
) -> (Result<Event<A::Value>>, State<A::Value>) | ||
where | ||
A: ApiMode, | ||
A::Value: Resource + 'static, | ||
{ | ||
loop { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this commented as a mistake?