From be463e094799e23a9b6e9df32330ed0b5062f84b Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 17 Jul 2017 14:18:52 +0300 Subject: [PATCH] messaging test --- package.json | 3 +- project.clj | 17 +++-- src/status_im/protocol/core.cljs | 11 ++-- src/status_im/protocol/handlers.cljs | 54 +++++++-------- src/status_im/protocol/web3/keys.cljs | 6 -- src/status_im/protocol/web3/utils.cljs | 5 -- src/status_im/utils/web3_provider.cljs | 8 ++- test/cljs/status_im/test/protocol/core.cljs | 65 +++++++++++++++++++ test/cljs/status_im/test/protocol/node.cljs | 44 +++++++++++++ test/cljs/status_im/test/protocol/runner.cljs | 13 ++++ test/cljs/status_im/test/protocol/utils.cljs | 26 ++++++++ 11 files changed, 202 insertions(+), 50 deletions(-) create mode 100644 test/cljs/status_im/test/protocol/core.cljs create mode 100644 test/cljs/status_im/test/protocol/node.cljs create mode 100644 test/cljs/status_im/test/protocol/runner.cljs create mode 100644 test/cljs/status_im/test/protocol/utils.cljs diff --git a/package.json b/package.json index 5d062cc294..a49217a068 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "postinstall": "./postinstall.sh" }, "devDependencies": { - "rn-nodeify": "^6.0.1" + "rn-nodeify": "^6.0.1", + "sleep": "^5.1.1" }, "dependencies": { "assert": "^1.4.1", diff --git a/project.clj b/project.clj index 68f90a43d4..7e57f6a1d9 100644 --- a/project.clj +++ b/project.clj @@ -8,7 +8,7 @@ [reagent "0.6.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]] [re-frame "0.9.4"] [natal-shell "0.3.0"] - [com.andrewmcveigh/cljs-time "0.4.0"] + [com.andrewmcveigh/cljs-time "0.5.0"] [tailrecursion/cljs-priority-map "1.2.0"] [com.taoensso/timbre "4.7.4"] [com.google.guava/guava "21.0"]] @@ -22,7 +22,8 @@ ["with-profile" "prod" "cljsbuild" "once" "ios"] ["with-profile" "prod" "cljsbuild" "once" "android"]] "generate-externs" ["with-profile" "prod" "externs" "android" "externs/externs.js"] - "test-cljs" ["with-profile" "test" "doo" "node" "test" "once"]} + "test-cljs" ["with-profile" "test" "doo" "node" "test" "once"] + "test-protocol" ["with-profile" "test" "doo" "node" "protocol" "once"]} :test-paths ["test/clj"] :figwheel {:nrepl-port 7888} :profiles {:dev {:dependencies [[figwheel-sidecar "0.5.11"] @@ -55,6 +56,14 @@ :source-paths ["src" "test/cljs"] :compiler {:main status-im.test.runner + :output-to "target/test/test.js" + :output-dir "target/test" + :optimizations :none + :target :nodejs}} + {:id "protocol" + :source-paths ["src" "test/cljs"] + :compiler + {:main status-im.test.protocol.runner :output-to "target/test/test.js" :output-dir "target/test" :optimizations :none @@ -67,7 +76,7 @@ :output-dir "target/ios-prod" :static-fns true :optimize-constants true - :optimizations :advanced + :optimizations :simple :externs ["externs/externs.js"] :closure-defines {"goog.DEBUG" false} :parallel-build true}} @@ -78,7 +87,7 @@ :output-dir "target/android-prod" :static-fns true :optimize-constants true - :optimizations :advanced + :optimizations :simple :externs ["externs/externs.js"] :closure-defines {"goog.DEBUG" false} :parallel-build true}}}}}}) diff --git a/src/status_im/protocol/core.cljs b/src/status_im/protocol/core.cljs index 96d64d3679..caa1623e03 100644 --- a/src/status_im/protocol/core.cljs +++ b/src/status_im/protocol/core.cljs @@ -50,7 +50,6 @@ (def message-pending? d/message-pending?) ;; initialization -(s/def ::rpc-url string?) (s/def ::identity string?) (s/def :message/chat-id string?) (s/def ::public? (s/and boolean? true?)) @@ -65,7 +64,7 @@ (s/def ::profile-keypair :message/keypair) (s/def ::options (s/merge - (s/keys :req-un [::rpc-url ::identity ::groups ::profile-keypair + (s/keys :req-un [::identity ::groups ::profile-keypair ::callback :discoveries/hashtags ::contacts]) ::d/delivery-options)) @@ -73,15 +72,14 @@ (def reset-all-pending-messages! d/reset-all-pending-messages!) (defn init-whisper! - [{:keys [rpc-url identity groups callback + [{:keys [identity groups callback web3 contacts profile-keypair pending-messages] :as options}] {:pre [(valid? ::options options)]} (debug :init-whisper) (stop-watching-all!) (d/reset-all-pending-messages!) - (let [web3 (u/make-web3 rpc-url) - listener-options {:web3 web3 + (let [listener-options {:web3 web3 :identity identity :callback callback}] ;; start listening to groups @@ -110,5 +108,4 @@ web3 (assoc options :online-message online-message))) (doseq [pending-message pending-messages] - (d/add-prepeared-pending-message! web3 pending-message)) - web3)) + (d/add-prepeared-pending-message! web3 pending-message)))) diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index b8bed2f17b..e32722d0cb 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -18,7 +18,8 @@ [status-im.constants :as c] [status-im.components.status :as status] [clojure.string :refer [join]] - [status-im.utils.scheduler :as s])) + [status-im.utils.scheduler :as s] + [status-im.utils.web3-provider :as w3])) (register-handler :initialize-protocol (fn [db [_ current-account-id ethereum-rpc-url]] @@ -28,31 +29,32 @@ (if public-key (let [rpc-url (or ethereum-rpc-url c/ethereum-rpc-url) groups (chats/get-active-group-chats) - w3 (protocol/init-whisper! - {:rpc-url rpc-url - :identity public-key - :groups groups - :callback #(dispatch [:incoming-message %1 %2]) - :ack-not-received-s-interval 125 - :default-ttl 120 - :send-online-s-interval 180 - :ttl-config {:public-group-message 2400} - :max-attempts-number 3 - :delivery-loop-ms-interval 500 - :profile-keypair {:public updates-public-key - :private updates-private-key} - :hashtags (u/get-hashtags status) - :pending-messages (pending-messages/get-all) - :contacts (keep (fn [{:keys [whisper-identity - public-key - private-key]}] - (when (and public-key private-key) - {:identity whisper-identity - :keypair {:public public-key - :private private-key}})) - (contacts/get-all)) - :post-error-callback #(dispatch [::post-error %])})] - (assoc db :web3 w3 + web3 (w3/make-web3 rpc-url)] + (protocol/init-whisper! + {:rpc-url rpc-url + :identity public-key + :groups groups + :callback #(dispatch [:incoming-message %1 %2]) + :ack-not-received-s-interval 125 + :default-ttl 120 + :send-online-s-interval 180 + :ttl-config {:public-group-message 2400} + :max-attempts-number 3 + :delivery-loop-ms-interval 500 + :profile-keypair {:public updates-public-key + :private updates-private-key} + :hashtags (u/get-hashtags status) + :pending-messages (pending-messages/get-all) + :contacts (keep (fn [{:keys [whisper-identity + public-key + private-key]}] + (when (and public-key private-key) + {:identity whisper-identity + :keypair {:public public-key + :private private-key}})) + (contacts/get-all)) + :post-error-callback #(dispatch [::post-error %])}) + (assoc db :web3 web3 :rpc-url rpc-url)) db)))) diff --git a/src/status_im/protocol/web3/keys.cljs b/src/status_im/protocol/web3/keys.cljs index 689a8ba33a..ca1defdb6c 100644 --- a/src/status_im/protocol/web3/keys.cljs +++ b/src/status_im/protocol/web3/keys.cljs @@ -1,6 +1,5 @@ (ns status-im.protocol.web3.keys) -(defonce status-sym-key-id (atom nil)) (def status-key-password "status-key-password") (def status-group-key-password "status-public-group-key-password") @@ -20,8 +19,3 @@ (fn [err res] (swap! password->keys assoc password res) (callback res))))) - - -(defn get-status-sym-key - [web3 callback] - (get-sym-key web3 status-key-password callback)) diff --git a/src/status_im/protocol/web3/utils.cljs b/src/status_im/protocol/web3/utils.cljs index 683b033730..6e36f3cd35 100644 --- a/src/status_im/protocol/web3/utils.cljs +++ b/src/status_im/protocol/web3/utils.cljs @@ -16,10 +16,5 @@ (defn shh [web3] (.-shh web3)) -(defn make-web3 [rpc-url] - (->> rpc-url - w3/get-provider - dependencies/Web3.)) - (defn timestamp [] (to-long (now))) diff --git a/src/status_im/utils/web3_provider.cljs b/src/status_im/utils/web3_provider.cljs index 83541d041e..c4e43c0611 100644 --- a/src/status_im/utils/web3_provider.cljs +++ b/src/status_im/utils/web3_provider.cljs @@ -1,6 +1,7 @@ (ns status-im.utils.web3-provider (:require [taoensso.timbre :as log] - [status-im.components.status :as status])) + [status-im.components.status :as status] + [status-im.js-dependencies :as dependencies])) (defn get-provider [rpc-url] #js {:sendAsync (fn [payload callback] @@ -11,3 +12,8 @@ (if (= "" response) (log/warn :web3-response-error) (callback nil (.parse js/JSON response))))))}) + +(defn make-web3 [rpc-url] + (->> rpc-url + get-provider + dependencies/Web3.)) diff --git a/test/cljs/status_im/test/protocol/core.cljs b/test/cljs/status_im/test/protocol/core.cljs new file mode 100644 index 0000000000..1bbcd2e338 --- /dev/null +++ b/test/cljs/status_im/test/protocol/core.cljs @@ -0,0 +1,65 @@ +(ns status-im.test.protocol.core + (:require [cljs.test :refer-macros [deftest is testing run-tests async]] + [cljs.nodejs :as nodejs] + [status-im.test.protocol.node :as node] + [status-im.test.protocol.utils :as utils] + [status-im.protocol.core :as protocol])) + +(nodejs/enable-util-print!) + +(node/prepare-env!) + +(def rpc-url "http://localhost:8645") +(def Web3 (js/require "web3")) +(defn make-web3 [] + (Web3. (Web3.providers.HttpProvider. rpc-url))) + +(defn make-callback [identity done] + (fn [& args] + (is (contains? #{:sent :pending} (first args))) + (when (= (first args) :sent) + (protocol/reset-all-pending-messages!) + (protocol/stop-watching-all!) + (node/stop!) + (done) + (utils/exit!)))) + +(defn post-error-callback [identity] + (fn [& args] + (.log js/console (str :post-error " " identity "\n" args)))) + +(defn id-specific-config + [id {:keys [private public]} contacts done] + {:identity id + :callback (make-callback id done) + :profile-keypair {:public public + :private private} + :contacts contacts + :post-error-callback (post-error-callback id)}) + +(deftest test-send-message! + (async done + (node/start!) + (let [web3 (make-web3) + id1-keypair (protocol/new-keypair!) + common-config {:web3 web3 + :groups [] + :ack-not-received-s-interval 125 + :default-ttl 120 + :send-online-s-interval 180 + :ttl-config {:public-group-message 2400} + :max-attempts-number 3 + :delivery-loop-ms-interval 500 + :hashtags [] + :pending-messages []} + id1-config (id-specific-config node/identity-1 id1-keypair [] done)] + (protocol/init-whisper! (merge common-config id1-config)) + (protocol/send-message! + {:web3 web3 + :message {:message-id "123" + :from node/identity-1 + :to node/identity-2 + :payload {:type :message + :content-type "text/plain" + :content "123" + :timestamp 1498723691404}}})))) diff --git a/test/cljs/status_im/test/protocol/node.cljs b/test/cljs/status_im/test/protocol/node.cljs new file mode 100644 index 0000000000..86b67d710e --- /dev/null +++ b/test/cljs/status_im/test/protocol/node.cljs @@ -0,0 +1,44 @@ +(ns status-im.test.protocol.node + (:require [clojure.string :as s] + [status-im.test.protocol.utils :as utils])) + +(def build-dir "target") + +(defonce node-process (atom nil)) + +(defn prepare-env! [] + (when-not (utils/exist? build-dir) + (println "mkdir " build-dir) + (utils/mkdir-sync build-dir)) + (let [dir (s/join "/" [build-dir "status-go"]) + opts #js {:cwd dir}] + (if-not (utils/exist? dir) + (utils/exec-sync "git clone https://github.com/status-im/status-go.git -b bug/whisper-on-geth1.6.1" #js {:cwd build-dir}) + (utils/exec-sync "git pull origin bug/whisper-on-geth1.6.1" opts)) + (println "Compile statusgo...") + (utils/exec-sync "make statusgo" opts) + (println "Done."))) + +(defn start! [] + (when-not @node-process + (println "Start wnode...") + (let [dir (s/join "/" [build-dir "status-go" "build" "bin"])] + (let [proc (utils/spawn "./statusd" + ["wnode" "--http" "--httpport" "8645"] + {:cwd dir})] + (reset! node-process proc) + (utils/sleep 5) + (println "Done."))))) + + +(defn stop! [] + (println "Stop wnode...") + (.kill @node-process) + (println "Done.") + (reset! node-process nil)) + +(def identity-1 "0x04eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae3") +(def identity-2 "0x0490161b00f2c47542d28c2e8908e77159b1720dccceb6393d7c001850122efc3b1709bcea490fd8f5634ba1a145aa0722d86b9330b0e39a8d493cb981fd459da2") +(def topic-1 "0xdeadbeef") + +(def topic-2 "0xbeefdead") diff --git a/test/cljs/status_im/test/protocol/runner.cljs b/test/cljs/status_im/test/protocol/runner.cljs new file mode 100644 index 0000000000..efaee67000 --- /dev/null +++ b/test/cljs/status_im/test/protocol/runner.cljs @@ -0,0 +1,13 @@ +(ns status-im.test.protocol.runner + (:require [doo.runner :refer-macros [doo-tests]] + [status-im.test.protocol.core])) + +(enable-console-print!) + +;; Or doo will exit with an error, see: +;; https://github.com/bensu/doo/issues/83#issuecomment-165498172 +(set! (.-error js/console) (fn [x] (.log js/console x))) + +(set! goog.DEBUG false) + +(doo-tests 'status-im.test.protocol.core) diff --git a/test/cljs/status_im/test/protocol/utils.cljs b/test/cljs/status_im/test/protocol/utils.cljs new file mode 100644 index 0000000000..2b800b0040 --- /dev/null +++ b/test/cljs/status_im/test/protocol/utils.cljs @@ -0,0 +1,26 @@ +(ns status-im.test.protocol.utils) + +(def fs (js/require "fs")) +(def child-process (js/require "child_process")) +(def process (js/require "process")) +(def sleep (.-sleep (js/require "sleep"))) + +(def exist? (.-existsSync fs)) + +(defn exec-sync [command options] + (.execSync child-process command (clj->js options))) + +(defn exec [command options] + (.exec child-process command (clj->js options))) + +(defn spawn-sync [command args options] + (.spawnSync child-process command (clj->js args) (clj->js options))) + +(defn spawn [command args options] + (.spawn child-process command (clj->js args) (clj->js options))) + +(defn exit! [] + (.exit process)) + +(defn mkdir-sync [dir] + (.mkdirSync fs dir))