Factor out some UTs code in test-utils.js
[poolifier.git] / tests / test-utils.js
index 690f4b37487cf10d369b5cc1589727b358a5f0f7..ab57685eb58fc000099fe4beec1287c01fcff14a 100644 (file)
@@ -16,6 +16,57 @@ class TestUtils {
   static async sleep (ms) {
     return new Promise(resolve => setTimeout(resolve, ms))
   }
+
+  static async workerSleepFunction (
+    data,
+    ms,
+    rejection = false,
+    rejectionMessage = ''
+  ) {
+    return new Promise((resolve, reject) => {
+      setTimeout(
+        () =>
+          rejection === true
+            ? reject(new Error(rejectionMessage))
+            : resolve(data),
+        ms
+      )
+    })
+  }
+
+  static jsonIntegerSerialization (n) {
+    for (let i = 0; i < n; i++) {
+      const o = {
+        a: i
+      }
+      JSON.stringify(o)
+    }
+  }
+
+  /**
+   * Intentionally inefficient implementation.
+   *
+   * @param {number} n
+   * @returns {number}
+   */
+  static fibonacci (n) {
+    if (n <= 1) return 1
+    return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2)
+  }
+
+  /**
+   * Intentionally inefficient implementation.
+   *
+   * @param {number} n
+   * @returns {number}
+   */
+  static factorial (n) {
+    if (n === 0) {
+      return 1
+    } else {
+      return TestUtils.factorial(n - 1) * n
+    }
+  }
 }
 
 module.exports = TestUtils