Browse Source

Implement updates test for warp-rust (#6359)

Konrad Borowski 4 years ago
parent
commit
253d25dc8e

+ 1 - 0
frameworks/Rust/warp-rust/README.md

@@ -11,6 +11,7 @@ warp is a composable web server framework based on hyper.
 * [DB](src/main.rs)
 * [QUERIES](src/main.rs)
 * [FORTUNES](src/main.rs)
+* [UPDATE](src/main.rs)
 
 ## Test URLs
 ### JSON

+ 1 - 0
frameworks/Rust/warp-rust/config.toml

@@ -7,6 +7,7 @@ urls.json = "/json"
 urls.db = "/db"
 urls.query = "/queries/"
 urls.fortune = "/fortunes"
+urls.update = "/updates/"
 approach = "Realistic"
 classification = "Micro"
 database = "Postgres"

+ 34 - 1
frameworks/Rust/warp-rust/src/main.rs

@@ -66,7 +66,7 @@ fn db(pool: &'static PgPool) -> impl Filter<Extract = impl Reply, Error = Reject
 
 fn queries(pool: &'static PgPool) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
     let between = Uniform::from(1..=10_000);
-    let clamped = warp::path!(u32).map(|queries: u32| queries.max(1).min(500));
+    let clamped = warp::path!(u32).map(|queries: u32| queries.clamp(1, 500));
     warp::path!("queries" / ..)
         .and(clamped.or(warp::any().map(|| 1)).unify())
         .and_then(move |queries| {
@@ -109,12 +109,45 @@ fn fortune(pool: &'static PgPool) -> impl Filter<Extract = impl Reply, Error = R
     })
 }
 
+fn update(pool: &'static PgPool) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    let between = Uniform::from(1..=10_000);
+    let clamped = warp::path!(u32).map(|queries: u32| queries.clamp(1, 500));
+    warp::path!("updates" / ..)
+        .and(clamped.or(warp::any().map(|| 1)).unify())
+        .and_then(move |queries| async move {
+            let mut worlds = with_rng(|rng| {
+                (0..queries)
+                    .map(|_| World::get_by_id(pool, between.sample(rng)))
+                    .collect::<FuturesUnordered<_>>()
+                    .collect::<Vec<_>>()
+            })
+            .await;
+            with_rng(|rng| {
+                for world in &mut worlds {
+                    world.randomnumber = between.sample(rng);
+                }
+            });
+            let mut transaction = pool.begin().await.unwrap();
+            for world in &worlds {
+                sqlx::query("UPDATE world SET randomnumber = $1 WHERE id = $2")
+                    .bind(world.randomnumber)
+                    .bind(world.id)
+                    .execute(&mut transaction)
+                    .await
+                    .unwrap();
+            }
+            transaction.commit().await.unwrap();
+            Ok::<_, Rejection>(warp::reply::json(&worlds))
+        })
+}
+
 fn routes(pool: &'static PgPool) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
     json()
         .or(plaintext())
         .or(db(pool))
         .or(queries(pool))
         .or(fortune(pool))
+        .or(update(pool))
         .map(|reply| warp::reply::with_header(reply, header::SERVER, "warp"))
 }
 

+ 1 - 1
frameworks/Rust/warp-rust/warp-rust.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.49
+FROM rust:1.50
 
 WORKDIR /warp-rust
 COPY src src