messaging test

This commit is contained in:
Roman Volosovskyi 2017-07-17 14:18:52 +03:00
parent 0bf8b711be
commit be463e0947
11 changed files with 202 additions and 50 deletions

View File

@ -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",

View File

@ -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}}}}}})

View File

@ -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))))

View File

@ -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))))

View File

@ -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))

View File

@ -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)))

View File

@ -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.))

View File

@ -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}}}))))

View File

@ -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")

View File

@ -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)

View File

@ -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))