Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeam committed Apr 12, 2023
1 parent 134cbf8 commit 811650c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
103 changes: 52 additions & 51 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,34 +1544,28 @@ mod test {
#[tokio::test]
async fn should_reset_the_connection() -> super::Result<()> {
let mut conn = Conn::new(get_opts()).await?;
let max_execution_time = conn
.query_first::<u64, _>("SELECT @@max_execution_time")
.await?
.unwrap();

conn.exec_drop(
"SET SESSION max_execution_time = ?",
(max_execution_time + 1,),
)
.await?;
assert_eq!(
conn.query_first::<Value, _>("SELECT @foo").await?.unwrap(),
Value::NULL
);

conn.query_drop("SET @foo = 'foo'").await?;

assert_eq!(
conn.query_first::<u64, _>("SELECT @@max_execution_time")
.await?,
Some(max_execution_time + 1)
conn.query_first::<String, _>("SELECT @foo").await?.unwrap(),
"foo",
);

if conn.reset().await? {
assert_eq!(
conn.query_first::<u64, _>("SELECT @@max_execution_time")
.await?,
Some(max_execution_time)
conn.query_first::<Value, _>("SELECT @foo").await?.unwrap(),
Value::NULL
);
} else {
assert_eq!(
conn.query_first::<u64, _>("SELECT @@max_execution_time")
.await?,
Some(max_execution_time + 1)
conn.query_first::<String, _>("SELECT @foo").await?.unwrap(),
"foo",
);
}

Expand All @@ -1582,28 +1576,22 @@ mod test {
#[tokio::test]
async fn should_change_user() -> super::Result<()> {
let mut conn = Conn::new(get_opts()).await?;
let max_execution_time = conn
.query_first::<u64, _>("SELECT @@max_execution_time")
.await?
.unwrap();
assert_eq!(
conn.query_first::<Value, _>("SELECT @foo").await?.unwrap(),
Value::NULL
);

conn.exec_drop(
"SET SESSION max_execution_time = ?",
(max_execution_time + 1,),
)
.await?;
conn.query_drop("SET @foo = 'foo'").await?;

assert_eq!(
conn.query_first::<u64, _>("SELECT @@max_execution_time")
.await?,
Some(max_execution_time + 1)
conn.query_first::<String, _>("SELECT @foo").await?.unwrap(),
"foo",
);

conn.change_user(Default::default()).await?;
assert_eq!(
conn.query_first::<u64, _>("SELECT @@max_execution_time")
.await?,
Some(max_execution_time)
conn.query_first::<Value, _>("SELECT @foo").await?.unwrap(),
Value::NULL
);

let plugins: &[&str] = if !conn.inner.is_mariadb && conn.server_version() >= (5, 8, 0) {
Expand All @@ -1613,42 +1601,55 @@ mod test {
};

for plugin in plugins {
let mut conn2 = Conn::new(get_opts()).await.unwrap();

let mut rng = rand::thread_rng();
let mut pass = [0u8; 10];
pass.try_fill(&mut rng).unwrap();
let pass: String = IntoIterator::into_iter(pass)
.map(|x| ((x % (123 - 97)) + 97) as char)
.collect();
conn.query_drop("DROP USER IF EXISTS __mysql_async_test_user")

conn.query_drop("DELETE FROM mysql.user WHERE user = '__mats'")
.await
.unwrap();
conn.query_drop(format!(
"CREATE USER '__mysql_async_test_user'@'%' IDENTIFIED WITH {} BY {}",
plugin,
Value::from(pass.clone()).as_sql(false)
))
.await
.unwrap();
conn.query_drop("FLUSH PRIVILEGES").await.unwrap();

if conn.inner.is_mariadb || conn.server_version() < (5, 7, 0) {
conn.query_drop("CREATE USER '__mats'@'%'").await.unwrap();
conn.query_drop(format!(
"SET PASSWORD FOR '__mats'@'%' = PASSWORD({})",
Value::from(pass.clone()).as_sql(false)
))
.await
.unwrap();
} else {
conn.query_drop(format!(
"CREATE USER '__mats'@'%' IDENTIFIED WITH {} BY {}",
plugin,
Value::from(pass.clone()).as_sql(false)
))
.await
.unwrap();
};

conn.query_drop("FLUSH PRIVILEGES").await.unwrap();

let mut conn2 = Conn::new(get_opts()).await.unwrap();
conn2
.change_user(
ChangeUserOpts::default()
.with_db_name(None)
.with_user(Some("__mysql_async_test_user".into()))
.with_user(Some("__mats".into()))
.with_pass(Some(pass)),
)
.await
.unwrap();
assert_eq!(
conn2
.query_first::<(Option<String>, String), _>("SELECT DATABASE(), USER();")
.await
.unwrap(),
Some((None, String::from("__mysql_async_test_user@localhost"))),
);
let (db, user) = conn2
.query_first::<(Option<String>, String), _>("SELECT DATABASE(), USER();")
.await
.unwrap()
.unwrap();
assert_eq!(db, None);
assert!(user.starts_with("__mats"));

conn2.disconnect().await.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/conn/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ mod test {
let _ = conns.pop();

// then, wait for a bit to let the connection be reclaimed
sleep(Duration::from_millis(50)).await;
sleep(Duration::from_millis(500)).await;

// now check that we have the expected # of connections
// this may look a little funky, but think of it this way:
Expand Down

0 comments on commit 811650c

Please sign in to comment.