diff options
author | Aleksey Kladov <[email protected]> | 2018-09-08 11:15:01 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-08 11:15:01 +0100 |
commit | 7daaddb2ac281dcad3ac99496b1cf3f06840887d (patch) | |
tree | 3a6c9ac35c0f198c782591b50eb50225769955bb /crates/server/tests | |
parent | 326ffcefe09906560a03d3184a2ce76841448702 (diff) |
Some abstraction around workers
Diffstat (limited to 'crates/server/tests')
-rw-r--r-- | crates/server/tests/heavy_tests/support.rs | 49 |
1 files changed, 23 insertions, 26 deletions
diff --git a/crates/server/tests/heavy_tests/support.rs b/crates/server/tests/heavy_tests/support.rs index 2710ab59b..355914033 100644 --- a/crates/server/tests/heavy_tests/support.rs +++ b/crates/server/tests/heavy_tests/support.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs, | 2 | fs, |
3 | thread, | ||
4 | cell::{Cell, RefCell}, | 3 | cell::{Cell, RefCell}, |
5 | path::PathBuf, | 4 | path::PathBuf, |
6 | time::Duration, | 5 | time::Duration, |
@@ -8,7 +7,7 @@ use std::{ | |||
8 | }; | 7 | }; |
9 | 8 | ||
10 | use tempdir::TempDir; | 9 | use tempdir::TempDir; |
11 | use crossbeam_channel::{after, Sender, Receiver}; | 10 | use crossbeam_channel::{after, Receiver}; |
12 | use flexi_logger::Logger; | 11 | use flexi_logger::Logger; |
13 | use languageserver_types::{ | 12 | use languageserver_types::{ |
14 | Url, | 13 | Url, |
@@ -22,7 +21,7 @@ use serde::Serialize; | |||
22 | use serde_json::{Value, from_str, to_string_pretty}; | 21 | use serde_json::{Value, from_str, to_string_pretty}; |
23 | use gen_lsp_server::{RawMessage, RawRequest, RawNotification}; | 22 | use gen_lsp_server::{RawMessage, RawRequest, RawNotification}; |
24 | 23 | ||
25 | use m::{Result, main_loop, req, thread_watcher::worker_chan}; | 24 | use m::{main_loop, req, thread_watcher::{ThreadWatcher, Worker}}; |
26 | 25 | ||
27 | pub fn project(fixture: &str) -> Server { | 26 | pub fn project(fixture: &str) -> Server { |
28 | static INIT: Once = Once::new(); | 27 | static INIT: Once = Once::new(); |
@@ -61,28 +60,27 @@ pub struct Server { | |||
61 | req_id: Cell<u64>, | 60 | req_id: Cell<u64>, |
62 | messages: RefCell<Vec<RawMessage>>, | 61 | messages: RefCell<Vec<RawMessage>>, |
63 | dir: TempDir, | 62 | dir: TempDir, |
64 | sender: Option<Sender<RawMessage>>, | 63 | worker: Option<Worker<RawMessage, RawMessage>>, |
65 | receiver: Receiver<RawMessage>, | 64 | watcher: Option<ThreadWatcher>, |
66 | server: Option<thread::JoinHandle<Result<()>>>, | ||
67 | } | 65 | } |
68 | 66 | ||
69 | impl Server { | 67 | impl Server { |
70 | fn new(dir: TempDir, files: Vec<(PathBuf, String)>) -> Server { | 68 | fn new(dir: TempDir, files: Vec<(PathBuf, String)>) -> Server { |
71 | let path = dir.path().to_path_buf(); | 69 | let path = dir.path().to_path_buf(); |
72 | let ((msg_sender, msg_receiver), server) = { | 70 | let (worker, watcher) = Worker::<RawMessage, RawMessage>::spawn( |
73 | let (api, mut msg_receiver, mut msg_sender) = worker_chan::<RawMessage, RawMessage>(128); | 71 | "test server", |
74 | let server = thread::spawn(move || { | 72 | 128, |
73 | move |mut msg_receiver, mut msg_sender| { | ||
75 | main_loop(true, path, &mut msg_receiver, &mut msg_sender) | 74 | main_loop(true, path, &mut msg_receiver, &mut msg_sender) |
76 | }); | 75 | .unwrap() |
77 | (api, server) | 76 | } |
78 | }; | 77 | ); |
79 | let res = Server { | 78 | let res = Server { |
80 | req_id: Cell::new(1), | 79 | req_id: Cell::new(1), |
81 | dir, | 80 | dir, |
82 | messages: Default::default(), | 81 | messages: Default::default(), |
83 | sender: Some(msg_sender), | 82 | worker: Some(worker), |
84 | receiver: msg_receiver, | 83 | watcher: Some(watcher), |
85 | server: Some(server), | ||
86 | }; | 84 | }; |
87 | 85 | ||
88 | for (path, text) in files { | 86 | for (path, text) in files { |
@@ -140,7 +138,7 @@ impl Server { | |||
140 | fn send_request_(&self, r: RawRequest) -> Value | 138 | fn send_request_(&self, r: RawRequest) -> Value |
141 | { | 139 | { |
142 | let id = r.id; | 140 | let id = r.id; |
143 | self.sender.as_ref() | 141 | self.worker.as_ref() |
144 | .unwrap() | 142 | .unwrap() |
145 | .send(RawMessage::Request(r)); | 143 | .send(RawMessage::Request(r)); |
146 | while let Some(msg) = self.recv() { | 144 | while let Some(msg) = self.recv() { |
@@ -183,14 +181,14 @@ impl Server { | |||
183 | } | 181 | } |
184 | } | 182 | } |
185 | fn recv(&self) -> Option<RawMessage> { | 183 | fn recv(&self) -> Option<RawMessage> { |
186 | recv_timeout(&self.receiver) | 184 | recv_timeout(&self.worker.as_ref().unwrap().out) |
187 | .map(|msg| { | 185 | .map(|msg| { |
188 | self.messages.borrow_mut().push(msg.clone()); | 186 | self.messages.borrow_mut().push(msg.clone()); |
189 | msg | 187 | msg |
190 | }) | 188 | }) |
191 | } | 189 | } |
192 | fn send_notification(&self, not: RawNotification) { | 190 | fn send_notification(&self, not: RawNotification) { |
193 | self.sender.as_ref() | 191 | self.worker.as_ref() |
194 | .unwrap() | 192 | .unwrap() |
195 | .send(RawMessage::Notification(not)); | 193 | .send(RawMessage::Notification(not)); |
196 | } | 194 | } |
@@ -198,16 +196,15 @@ impl Server { | |||
198 | 196 | ||
199 | impl Drop for Server { | 197 | impl Drop for Server { |
200 | fn drop(&mut self) { | 198 | fn drop(&mut self) { |
201 | { | 199 | self.send_request::<Shutdown>(666, ()); |
202 | self.send_request::<Shutdown>(666, ()); | 200 | let receiver = self.worker.take().unwrap().stop(); |
203 | drop(self.sender.take().unwrap()); | 201 | while let Some(msg) = recv_timeout(&receiver) { |
204 | while let Some(msg) = recv_timeout(&self.receiver) { | 202 | drop(msg); |
205 | drop(msg); | ||
206 | } | ||
207 | } | 203 | } |
208 | self.server.take() | 204 | self.watcher.take() |
209 | .unwrap() | 205 | .unwrap() |
210 | .join().unwrap().unwrap(); | 206 | .stop() |
207 | .unwrap(); | ||
211 | } | 208 | } |
212 | } | 209 | } |
213 | 210 | ||