Forráskód Böngészése

observable testing

Enzo 1 éve
szülő
commit
8154f40384
2 módosított fájl, 86 hozzáadás és 1 törlés
  1. 2 1
      package.json
  2. 84 0
      test/test3.ts

+ 2 - 1
package.json

@@ -9,7 +9,8 @@
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "node index.js",
     "test1": "node test/test1.js",
-    "test2": "node test/test2.js"
+    "test2": "node test/test2.js",
+    "test3": "node test/test3.js"
   },
   "repository": {
     "type": "git",

+ 84 - 0
test/test3.ts

@@ -0,0 +1,84 @@
+// Test Observable //
+
+import mongoose, { Model } from "mongoose";
+import { Subject, map } from "rxjs";
+const used = process.memoryUsage();
+
+let MongooseConnection: mongoose.Connection
+let connectionStatus = 0
+
+let mongoStorage: any = {
+    type: `MongoDB`,
+    url: `mongodb://192.168.100.59:27017/fromEnzo`
+}
+
+let data: any[] = []
+
+// Conect to designated storage destination
+async function connectMongo(storage: Storage) {
+    return new Promise((resolve, reject) => {
+        try {
+            console.log(`Connecting to ${storage.url}`)
+            MongooseConnection = mongoose.createConnection(storage.url)
+            connectionStatus = 1
+            resolve(connectionStatus)
+        }
+        catch (error) {
+            connectionStatus = 0
+            console.error('An error occurred while connecting to the database:', error);
+            setTimeout(() => {
+                connectMongo(storage).then(() => {
+                    resolve(connectionStatus)
+                })
+                console.log(`Reconnecting...`)
+            }, 3000);
+        }
+    })
+}
+
+async function getMongoData(storage: Storage) {
+    return new Promise<any>(async (resolve, reject) => {
+        await connectMongo(storage);
+        const Message: Model<any> = MongooseConnection.model('Message', require('../types/message.schema'));
+        try {
+            data = await Message.find().limit(10000) 
+            resolve(data)
+        } catch (err) {
+            console.error(err);
+        }
+    })
+}
+
+
+let testSubject: Subject<any> = new Subject()
+
+getMongoData(mongoStorage).then((data) => {
+    data.forEach(element => {
+        testSubject.next(element)
+        // console.log(`Memory usage: ${used.heapUsed / 1024 / 1024} MB`);
+    })
+})
+
+let count = 0
+
+testSubject.subscribe({
+    next: (e) => {
+        count++
+        console.log(count)
+    }
+});
+
+let pipedObs = testSubject.pipe(
+    map((element) => {
+        let finalResponse: any = {
+            message: 'Piped element'
+        }
+        return finalResponse
+    })
+)
+
+pipedObs.subscribe(element => {
+    count ++
+    console.log(element.message + count)
+})
+