From 87fdcdc131934c3337ac9b442b28255710969a6b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 29 Jan 2020 13:11:22 -0500 Subject: [PATCH] dedup inspector client/channel test implementations (#262) --- tests/test_api.rs | 266 ++++++++++++++++++---------------------------- 1 file changed, 104 insertions(+), 162 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index 511abf02..332ca87a 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -2261,6 +2261,97 @@ fn try_from_local() { } } +struct ClientCounter { + base: v8::inspector::V8InspectorClientBase, + count_run_message_loop_on_pause: usize, + count_quit_message_loop_on_pause: usize, + count_run_if_waiting_for_debugger: usize, +} + +impl ClientCounter { + fn new() -> Self { + Self { + base: v8::inspector::V8InspectorClientBase::new::(), + count_run_message_loop_on_pause: 0, + count_quit_message_loop_on_pause: 0, + count_run_if_waiting_for_debugger: 0, + } + } +} + +impl v8::inspector::V8InspectorClientImpl for ClientCounter { + fn base(&self) -> &v8::inspector::V8InspectorClientBase { + &self.base + } + + fn base_mut(&mut self) -> &mut v8::inspector::V8InspectorClientBase { + &mut self.base + } + + fn run_message_loop_on_pause(&mut self, context_group_id: i32) { + assert_eq!(context_group_id, 1); + self.count_run_message_loop_on_pause += 1; + } + + fn quit_message_loop_on_pause(&mut self) { + self.count_quit_message_loop_on_pause += 1; + } + + fn run_if_waiting_for_debugger(&mut self, context_group_id: i32) { + assert_eq!(context_group_id, 1); + self.count_run_message_loop_on_pause += 1; + } +} + +struct ChannelCounter { + base: v8::inspector::ChannelBase, + count_send_response: usize, + count_send_notification: usize, + count_flush_protocol_notifications: usize, +} + +impl ChannelCounter { + pub fn new() -> Self { + Self { + base: v8::inspector::ChannelBase::new::(), + count_send_response: 0, + count_send_notification: 0, + count_flush_protocol_notifications: 0, + } + } +} + +impl v8::inspector::ChannelImpl for ChannelCounter { + fn base(&self) -> &v8::inspector::ChannelBase { + &self.base + } + fn base_mut(&mut self) -> &mut v8::inspector::ChannelBase { + &mut self.base + } + fn send_response( + &mut self, + call_id: i32, + message: v8::UniquePtr, + ) { + println!( + "send_response call_id {} message {}", + call_id, + message.unwrap().string() + ); + self.count_send_response += 1; + } + fn send_notification( + &mut self, + message: v8::UniquePtr, + ) { + println!("send_notificatio message {}", message.unwrap().string()); + self.count_send_notification += 1; + } + fn flush_protocol_notifications(&mut self) { + self.count_flush_protocol_notifications += 1; + } +} + #[test] fn inspector_dispatch_protocol_message() { let _setup_guard = setup(); @@ -2272,79 +2363,18 @@ fn inspector_dispatch_protocol_message() { use v8::inspector::*; - struct Client { - base: V8InspectorClientBase, - } - - impl Client { - fn new() -> Self { - Self { - base: V8InspectorClientBase::new::(), - } - } - } - - impl V8InspectorClientImpl for Client { - fn base(&self) -> &V8InspectorClientBase { - &self.base - } - fn base_mut(&mut self) -> &mut V8InspectorClientBase { - &mut self.base - } - } - - struct TestChannel { - base: ChannelBase, - send_response_count: usize, - send_notification_count: usize, - flush_protocol_notifications_count: usize, - } - - impl TestChannel { - pub fn new() -> Self { - Self { - base: ChannelBase::new::(), - send_response_count: 0, - send_notification_count: 0, - flush_protocol_notifications_count: 0, - } - } - } - - impl ChannelImpl for TestChannel { - fn base(&self) -> &ChannelBase { - &self.base - } - fn base_mut(&mut self) -> &mut ChannelBase { - &mut self.base - } - fn send_response( - &mut self, - _call_id: i32, - _message: v8::UniquePtr, - ) { - self.send_response_count += 1; - } - fn send_notification(&mut self, _message: v8::UniquePtr) { - self.send_notification_count += 1; - } - fn flush_protocol_notifications(&mut self) { - self.flush_protocol_notifications_count += 1; - } - } - let mut hs = v8::HandleScope::new(scope); let scope = hs.enter(); let context = v8::Context::new(scope); let mut cs = v8::ContextScope::new(scope, context); let _scope = cs.enter(); - let mut default_client = Client::new(); + let mut default_client = ClientCounter::new(); let mut inspector = V8Inspector::create(&mut isolate, &mut default_client); let name = b""; let name_view = StringView::from(&name[..]); inspector.context_created(context, 1, &name_view); - let mut channel = TestChannel::new(); + let mut channel = ChannelCounter::new(); let state = b"{}"; let state_view = StringView::from(&state[..]); let mut session = inspector.connect(1, &mut channel, &state_view); @@ -2354,9 +2384,9 @@ fn inspector_dispatch_protocol_message() { let message = &message.into_bytes()[..]; let string_view = StringView::from(message); session.dispatch_protocol_message(&string_view); - assert_eq!(channel.send_response_count, 1); - assert_eq!(channel.send_notification_count, 0); - assert_eq!(channel.flush_protocol_notifications_count, 0); + assert_eq!(channel.count_send_response, 1); + assert_eq!(channel.count_send_notification, 0); + assert_eq!(channel.count_flush_protocol_notifications, 0); } #[test] @@ -2370,103 +2400,15 @@ fn inspector_schedule_pause_on_next_statement() { use v8::inspector::*; - struct Client { - base: V8InspectorClientBase, - count_run_message_loop_on_pause: usize, - count_quit_message_loop_on_pause: usize, - count_run_if_waiting_for_debugger: usize, - } - - impl Client { - fn new() -> Self { - Self { - base: V8InspectorClientBase::new::(), - count_run_message_loop_on_pause: 0, - count_quit_message_loop_on_pause: 0, - count_run_if_waiting_for_debugger: 0, - } - } - } - - impl V8InspectorClientImpl for Client { - fn base(&self) -> &V8InspectorClientBase { - &self.base - } - - fn base_mut(&mut self) -> &mut V8InspectorClientBase { - &mut self.base - } - - fn run_message_loop_on_pause(&mut self, context_group_id: i32) { - assert_eq!(context_group_id, 1); - self.count_run_message_loop_on_pause += 1; - } - - fn quit_message_loop_on_pause(&mut self) { - self.count_quit_message_loop_on_pause += 1; - } - - fn run_if_waiting_for_debugger(&mut self, context_group_id: i32) { - assert_eq!(context_group_id, 1); - self.count_run_message_loop_on_pause += 1; - } - } - - struct TestChannel { - base: ChannelBase, - send_response_count: usize, - send_notification_count: usize, - flush_protocol_notifications_count: usize, - } - - impl TestChannel { - pub fn new() -> Self { - Self { - base: ChannelBase::new::(), - send_response_count: 0, - send_notification_count: 0, - flush_protocol_notifications_count: 0, - } - } - } - - impl ChannelImpl for TestChannel { - fn base(&self) -> &ChannelBase { - &self.base - } - fn base_mut(&mut self) -> &mut ChannelBase { - &mut self.base - } - fn send_response( - &mut self, - call_id: i32, - message: v8::UniquePtr, - ) { - println!( - "send_response call_id {} message {}", - call_id, - message.unwrap().string() - ); - self.send_response_count += 1; - } - fn send_notification(&mut self, message: v8::UniquePtr) { - println!("send_notificatio message {}", message.unwrap().string()); - self.send_notification_count += 1; - } - fn flush_protocol_notifications(&mut self) { - self.flush_protocol_notifications_count += 1; - } - } - let mut hs = v8::HandleScope::new(scope); let scope = hs.enter(); let context = v8::Context::new(scope); let mut cs = v8::ContextScope::new(scope, context); let scope = cs.enter(); - let mut client = Client::new(); + let mut client = ClientCounter::new(); let mut inspector = V8Inspector::create(&mut isolate, &mut client); - let mut channel = TestChannel::new(); + let mut channel = ChannelCounter::new(); let state = b"{}"; let state_view = StringView::from(&state[..]); let mut session = inspector.connect(1, &mut channel, &state_view); @@ -2495,9 +2437,9 @@ fn inspector_schedule_pause_on_next_statement() { let detail = StringView::from(&detail[..]); session.schedule_pause_on_next_statement(&reason, &detail); - assert_eq!(channel.send_response_count, 1); - assert_eq!(channel.send_notification_count, 0); - assert_eq!(channel.flush_protocol_notifications_count, 0); + assert_eq!(channel.count_send_response, 1); + assert_eq!(channel.count_send_notification, 0); + assert_eq!(channel.count_flush_protocol_notifications, 0); assert_eq!(client.count_run_message_loop_on_pause, 0); assert_eq!(client.count_quit_message_loop_on_pause, 0); assert_eq!(client.count_run_if_waiting_for_debugger, 0); @@ -2505,9 +2447,9 @@ fn inspector_schedule_pause_on_next_statement() { let r = eval(scope, context, "1+2").unwrap(); assert!(r.is_number()); - assert_eq!(channel.send_response_count, 1); - assert_eq!(channel.send_notification_count, 3); - assert_eq!(channel.flush_protocol_notifications_count, 0); + assert_eq!(channel.count_send_response, 1); + assert_eq!(channel.count_send_notification, 3); + assert_eq!(channel.count_flush_protocol_notifications, 0); assert_eq!(client.count_run_message_loop_on_pause, 1); assert_eq!(client.count_quit_message_loop_on_pause, 0); assert_eq!(client.count_run_if_waiting_for_debugger, 0);