Skip to content

Commit e992965

Browse files
committed
test(contract-test): completed contract testing for subscribe
Completed set of contract tests for subscription event engine. refactor(subscribe-ee): refactored retry Refactored retry to be coupled with cancellable request. refactor(clippy): apply clippy suggestions
1 parent 3820f2b commit e992965

File tree

25 files changed

+648
-258
lines changed

25 files changed

+648
-258
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ getrandom = { version = "0.2", optional = true }
109109
ciborium = { version = "0.2.1", default-features = false, optional = true }
110110

111111
# subscribe
112-
tokio = { version = "1", optional = true, features = ["rt-multi-thread", "macros"] }
112+
tokio = { version = "1", optional = true, features = ["rt-multi-thread", "macros", "time"] }
113113
futures = { version = "0.3.28", optional = true }
114114
async-channel = { version = "1.8", optional = true }
115115

@@ -121,7 +121,7 @@ getrandom = { version = "0.2", features = ["js"] }
121121

122122
[dev-dependencies]
123123
async-trait = "0.1"
124-
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
124+
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
125125
wiremock = "0.5"
126126
env_logger = "0.10"
127127
cucumber = { version = "0.20.0", features = ["output-junit"] }

src/core/error.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//!
55
//! [`pubnub`]: ../index.html
66
7-
use crate::lib::{alloc::string::String, alloc::vec::Vec};
7+
use crate::{
8+
core::TransportResponse,
9+
lib::alloc::{boxed::Box, string::String, vec::Vec},
10+
};
811
use snafu::Snafu;
912

1013
/// PubNub error type
@@ -37,7 +40,7 @@ pub enum PubNubError {
3740
details: String,
3841

3942
/// Failed request HTTP status code.
40-
status: u16,
43+
response: Option<Box<TransportResponse>>,
4144
},
4245

4346
/// this error is returned when the publication of the request fails
@@ -132,6 +135,9 @@ pub enum PubNubError {
132135

133136
/// List of channel groups which is affected by error.
134137
affected_channel_groups: Option<Vec<String>>,
138+
139+
/// Raw service response.
140+
response: Option<Box<TransportResponse>>,
135141
},
136142
}
137143

@@ -140,8 +146,12 @@ impl PubNubError {
140146
///
141147
/// This function used to inform about not initialized request parameters or
142148
/// validation failure.
143-
#[cfg(any(feature = "publish", feature = "access"))]
144-
pub(crate) fn general_api_error<S>(message: S, status: Option<u16>) -> Self
149+
#[cfg(any(feature = "publish", feature = "access", feature = "subscribe"))]
150+
pub(crate) fn general_api_error<S>(
151+
message: S,
152+
status: Option<u16>,
153+
response: Option<Box<TransportResponse>>,
154+
) -> Self
145155
where
146156
S: Into<String>,
147157
{
@@ -151,6 +161,48 @@ impl PubNubError {
151161
service: None,
152162
affected_channels: None,
153163
affected_channel_groups: None,
164+
response,
165+
}
166+
}
167+
168+
/// Retrieve attached service response.
169+
#[cfg(any(feature = "publish", feature = "access", feature = "subscribe"))]
170+
pub(crate) fn transport_response(&self) -> Option<Box<TransportResponse>> {
171+
match self {
172+
PubNubError::API { response, .. } | PubNubError::Transport { response, .. } => {
173+
response.clone()
174+
}
175+
_ => None,
176+
}
177+
}
178+
179+
/// Attach service response.
180+
///
181+
/// For better understanding some errors may provide additional information
182+
/// right from service response.
183+
#[cfg(any(feature = "publish", feature = "access", feature = "subscribe"))]
184+
pub(crate) fn attach_response(self, service_response: TransportResponse) -> Self {
185+
match &self {
186+
PubNubError::API {
187+
status,
188+
message,
189+
service,
190+
affected_channels,
191+
affected_channel_groups,
192+
..
193+
} => PubNubError::API {
194+
status: *status,
195+
message: message.clone(),
196+
service: service.clone(),
197+
affected_channels: affected_channels.clone(),
198+
affected_channel_groups: affected_channel_groups.clone(),
199+
response: Some(Box::new(service_response)),
200+
},
201+
PubNubError::Transport { details, .. } => PubNubError::Transport {
202+
details: details.clone(),
203+
response: Some(Box::new(service_response)),
204+
},
205+
_ => self,
154206
}
155207
}
156208
}

src/core/error_response.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl From<APIErrorBody> for PubNubError {
2424
service: value.service(),
2525
affected_channels: value.affected_channels(),
2626
affected_channel_groups: value.affected_channel_groups(),
27+
response: None,
2728
}
2829
}
2930
}

src/core/event_engine/effect_dispatcher.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,18 @@ mod should {
234234
#[derive(Clone)]
235235
struct TestRuntime {}
236236

237+
#[async_trait::async_trait]
237238
impl Runtime for TestRuntime {
238239
fn spawn<R>(&self, _future: impl Future<Output = R> + Send + 'static)
239240
where
240241
R: Send + 'static,
241242
{
242243
// Do nothing.
243244
}
245+
246+
async fn sleep(self, _delay: u64) {
247+
// Do nothing.
248+
}
244249
}
245250

246251
#[test]

src/core/event_engine/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,18 @@ mod should {
313313
#[derive(Clone)]
314314
struct TestRuntime {}
315315

316+
#[async_trait::async_trait]
316317
impl Runtime for TestRuntime {
317318
fn spawn<R>(&self, future: impl Future<Output = R> + Send + 'static)
318319
where
319320
R: Send + 'static,
320321
{
321322
tokio::spawn(future);
322323
}
324+
325+
async fn sleep(self, delay: u64) {
326+
tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await
327+
}
323328
}
324329

325330
#[tokio::test]

0 commit comments

Comments
 (0)