ソースを参照

Use memory storage for relayer tests

Cesar Rodas 3 ヶ月 前
コミット
1744c937b7

+ 1 - 1
Cargo.lock

@@ -944,7 +944,7 @@ dependencies = [
  "futures-util",
  "log",
  "nostr-rs-client",
- "nostr-rs-rocksdb",
+ "nostr-rs-memory",
  "nostr-rs-storage-base",
  "nostr-rs-types",
  "rand",

+ 1 - 1
crates/relayer/Cargo.toml

@@ -22,4 +22,4 @@ rand = "0.8.5"
 log = "0.4.17"
 
 [dev-dependencies]
-nostr-rs-rocksdb = { path = "../storage/rocksdb" }
+nostr-rs-memory = { path = "../storage/memory" }

+ 84 - 12
crates/relayer/src/relayer.rs

@@ -272,12 +272,12 @@ impl<T: Storage> Relayer<T> {
 #[cfg(test)]
 mod test {
     use super::*;
-    use crate::get_id;
-    use nostr_rs_rocksdb::RocksDb;
+    use nostr_rs_memory::Memory;
     use nostr_rs_types::Request;
+    use serde_json::json;
 
-    async fn get_db(prefill: bool) -> RocksDb {
-        let db = RocksDb::new(format!("/tmp/db/{}", get_id())).expect("db");
+    async fn get_db(prefill: bool) -> Memory {
+        let db = Memory::default();
         if prefill {
             let events = include_str!("../tests/events.json")
                 .lines()
@@ -287,20 +287,68 @@ mod test {
             for event in events {
                 assert!(db.store(&event).await.expect("valid"));
             }
+
+            while db.is_flushing() {
+                tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
+            }
         }
         db
     }
 
     #[tokio::test]
     async fn serve_listener_from_local_db() {
-        let request: Request = serde_json::from_str("[
-                \"REQ\",\"1298169700973717\",
-                {\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"since\":1681928304},
-                {\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[1,3,6,7,9735],\"since\":1681928304},
-                {\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},
-                {\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},
-                {\"#e\":[\"2e72250d80e9b3fd30230b3db3ed7d22f15d266ed345c36700b01ec153c9e28a\",\"a5e3369c43daf2675ecbce18831e5f4e07db0d4dde0ef4f5698e645e4c46eed1\"],\"kinds\":[1,6,7,9735]}
-            ]").expect("valid object");
+        let request = serde_json::from_value(json!([
+          "REQ",
+          "1298169700973717",
+          {
+            "authors": [
+              "39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb"
+            ],
+            "since": 1681928304
+          },
+          {
+            "#p": [
+              "39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb"
+            ],
+            "kinds": [
+              1,
+              3,
+              6,
+              7,
+              9735
+            ],
+            "since": 1681928304
+          },
+          {
+            "#p": [
+              "39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb"
+            ],
+            "kinds": [
+              4
+            ]
+          },
+          {
+            "authors": [
+              "39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb"
+            ],
+            "kinds": [
+              4
+            ]
+          },
+          {
+            "#e": [
+              "2e72250d80e9b3fd30230b3db3ed7d22f15d266ed345c36700b01ec153c9e28a",
+              "a5e3369c43daf2675ecbce18831e5f4e07db0d4dde0ef4f5698e645e4c46eed1"
+            ],
+            "kinds": [
+              1,
+              6,
+              7,
+              9735
+            ]
+          }
+        ]))
+        .expect("valid object");
         let (relayer, _) = Relayer::new(Some(get_db(true).await), None).expect("valid relayer");
         let (connection, mut recv) = Connection::new_for_test();
         let _ = relayer.recv_request_from_client(&connection, request).await;
@@ -337,6 +385,30 @@ mod test {
                 .id
                 .to_string()
         );
+
+        // ev3 (again)
+        assert_eq!(
+            "e862fe23daf52ab09b36a37fa91ca3743e0c323e630e8627891212ca147c2da9",
+            recv.try_recv()
+                .expect("valid")
+                .as_event()
+                .expect("event")
+                .event
+                .id
+                .to_string()
+        );
+        // ev2 (again)
+        assert_eq!(
+            "2e72250d80e9b3fd30230b3db3ed7d22f15d266ed345c36700b01ec153c9e28a",
+            recv.try_recv()
+                .expect("valid")
+                .as_event()
+                .expect("event")
+                .event
+                .id
+                .to_string()
+        );
+
         // eod
         assert!(recv
             .try_recv()

+ 1 - 1
crates/storage/base/src/test.rs

@@ -24,7 +24,7 @@ where
     }
 
     while db.is_flushing() {
-        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
+        tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
     }
 }
 

+ 1 - 1
crates/storage/rocksdb/src/lib.rs

@@ -232,7 +232,7 @@ impl Storage for RocksDb {
                 .collect();
             (Some(ns), None, keys)
         } else if !query.references_to_public_key.is_empty() {
-            let ns = self.reference_to_cf_handle(ReferenceType::RefEvent)?;
+            let ns = self.reference_to_cf_handle(ReferenceType::RefPublicKey)?;
             let keys = std::mem::take(&mut query.references_to_public_key)
                 .into_iter()
                 .map(|c| c.take())