refactor: make `ServerFixture::restart_server` consume the fixture

This also avoids an additional channel lock.
pull/24376/head
Marco Neumann 2021-05-20 14:30:26 +02:00
parent 464667d8b8
commit e28c031f76
2 changed files with 14 additions and 15 deletions

View File

@ -68,7 +68,7 @@ use tokio::sync::Mutex;
/// testing. /// testing.
pub struct ServerFixture { pub struct ServerFixture {
server: Arc<TestServer>, server: Arc<TestServer>,
grpc_channel: std::sync::RwLock<tonic::transport::Channel>, grpc_channel: tonic::transport::Channel,
} }
/// Specifieds should we configure a server initially /// Specifieds should we configure a server initially
@ -133,12 +133,10 @@ impl ServerFixture {
} }
async fn create_common(server: Arc<TestServer>) -> Self { async fn create_common(server: Arc<TestServer>) -> Self {
let grpc_channel = std::sync::RwLock::new( let grpc_channel = server
server
.grpc_channel() .grpc_channel()
.await .await
.expect("The server should have been up"), .expect("The server should have been up");
);
ServerFixture { ServerFixture {
server, server,
@ -149,8 +147,7 @@ impl ServerFixture {
/// Return a channel connected to the gRPC API. Panics if the /// Return a channel connected to the gRPC API. Panics if the
/// server is not yet up /// server is not yet up
pub fn grpc_channel(&self) -> tonic::transport::Channel { pub fn grpc_channel(&self) -> tonic::transport::Channel {
let guard = self.grpc_channel.read().expect("getting gRPC channel lock"); self.grpc_channel.clone()
guard.clone()
} }
/// Return the url base of the grpc management API /// Return the url base of the grpc management API
@ -201,19 +198,21 @@ impl ServerFixture {
/// Restart test server. /// Restart test server.
/// ///
/// This will break all currently connected clients! /// This will break all currently connected clients!
pub async fn restart_server(&self) { pub async fn restart_server(self) -> Self {
self.server.restart().await; self.server.restart().await;
self.server self.server
.wait_until_ready(InitialConfig::SetWriterId) .wait_until_ready(InitialConfig::SetWriterId)
.await; .await;
let grpc_channel = self
let new_channel = self
.server .server
.grpc_channel() .grpc_channel()
.await .await
.expect("The server should have been up"); .expect("The server should have been up");
let mut guard = self.grpc_channel.write().expect("lock poisened");
*guard = new_channel; Self {
server: self.server,
grpc_channel,
}
} }
} }

View File

@ -45,7 +45,7 @@ async fn test_query_chunk_after_restart() {
assert_chunk_query_works(&fixture, &db_name).await; assert_chunk_query_works(&fixture, &db_name).await;
// restart server // restart server
fixture.restart_server().await; let fixture = fixture.restart_server().await;
// need to re-register the database // need to re-register the database
create_readable_database(&db_name, fixture.grpc_channel()).await; create_readable_database(&db_name, fixture.grpc_channel()).await;