Browse Source

Merge branch 'add-replication-test' of CoBox/cobox-group-base into development

kyphae 2 years ago
parent
commit
f537caa4bd
No known key found for this signature in database
3 changed files with 71 additions and 6 deletions
  1. 1
    1
      package.json
  2. 68
    2
      test/index.test.js
  3. 2
    3
      yarn.lock

+ 1
- 1
package.json View File

@@ -22,7 +22,7 @@
22 22
     "cobox-constants": "^1.0.0",
23 23
     "cobox-crypto": "git+https://ledger-git.dyne.org/CoBox/cobox-crypto#development",
24 24
     "debug": "^4.1.1",
25
-    "multifeed": "^5.1.0",
25
+    "multifeed": "git+https://github.com/kappa-db/multifeed#fix-keypair-issue",
26 26
     "random-access-file": "^2.1.3",
27 27
     "random-access-memory": "^3.1.1"
28 28
   },

+ 68
- 2
test/index.test.js View File

@@ -3,7 +3,7 @@ const crypto = require('cobox-crypto')
3 3
 const path = require('path')
4 4
 
5 5
 const Base = require('../')
6
-const { tmp, cleanup } = require('./util')
6
+const { replicate, tmp, cleanup } = require('./util')
7 7
 
8 8
 describe('basic', (context) => {
9 9
   context('valid', async (assert, next) => {
@@ -12,7 +12,6 @@ describe('basic', (context) => {
12 12
     var group = new Base(storage, address)
13 13
     assert.ok(group.config, 'has a config')
14 14
     assert.ok(group.address, 'group created')
15
-    assert.same(group.config.masterKey, group.masterKey, 'has master_key')
16 15
     assert.ok(Buffer.isBuffer(group.address), 'address is a buffer')
17 16
     assert.same(group.path, path.join(storage, address.toString('hex')))
18 17
     assert.ok(group.storage, 'has random access file storage')
@@ -57,4 +56,71 @@ describe('basic', (context) => {
57 56
 
58 57
     cleanup(storage, next)
59 58
   })
59
+
60
+  context('replicate', async (assert, next) => {
61
+    var storage1 = tmp(),
62
+      storage2 = tmp(),
63
+      address = crypto.address()
64
+
65
+    var base1 = new Base(storage1, address)
66
+    var base2 = new Base(storage2, address)
67
+
68
+    base1._initFeeds()
69
+    base2._initFeeds()
70
+
71
+    await base1.ready()
72
+    await base2.ready()
73
+
74
+    base1.multifeed.writer('local', (err, feed1) => {
75
+      assert.error(err, 'no error')
76
+
77
+      base2.multifeed.writer('local', (err, feed2) => {
78
+        assert.error(err, 'no error')
79
+
80
+        var dog = 'dog'
81
+        feed1.append(dog, (err, seq) => {
82
+          assert.error(err, 'no error')
83
+
84
+          var cat = 'cat'
85
+          feed2.append(cat, (err, seq) => {
86
+            assert.error(err, 'no error')
87
+
88
+            replicate(base1, base2, (err) => {
89
+              assert.error(err, 'no error')
90
+
91
+              var count = 0
92
+
93
+              var dup2 = base2.multifeed.feed(feed1.key)
94
+              assert.ok(dup2, 'gets feed')
95
+              assert.same(dup2.key.toString('hex'), feed1.key.toString('hex'), 'definitely the replicated feed')
96
+              assert.ok(dup2.length === 1, 'has replicated')
97
+
98
+              dup2.get(0, (err, msg) => {
99
+                assert.error(err, 'no error')
100
+                assert.same(msg.toString(), dog, 'contains the replicated message')
101
+                ++count
102
+                done()
103
+              })
104
+
105
+              var dup1 = base1.multifeed.feed(feed2.key)
106
+              assert.ok(dup1, 'gets feed')
107
+              assert.same(dup1.key.toString('hex'), feed2.key.toString('hex'), 'definitely the replicated feed')
108
+              assert.ok(dup1.length === 1, 'has replicated')
109
+
110
+              dup1.get(0, (err, msg) => {
111
+                assert.error(err, 'no error')
112
+                assert.same(msg.toString(), cat, 'contains the replicated message')
113
+                ++count
114
+                done()
115
+              })
116
+
117
+              function done () {
118
+                if (count === 2) return cleanup([storage1, storage2], next)
119
+              }
120
+            })
121
+          })
122
+        })
123
+      })
124
+    })
125
+  })
60 126
 })

+ 2
- 3
yarn.lock View File

@@ -1078,10 +1078,9 @@ ms@^2.1.1:
1078 1078
   resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
1079 1079
   integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
1080 1080
 
1081
-multifeed@^5.1.0:
1081
+"multifeed@git+https://github.com/kappa-db/multifeed#fix-keypair-issue":
1082 1082
   version "5.1.0"
1083
-  resolved "https://registry.yarnpkg.com/multifeed/-/multifeed-5.1.0.tgz#71fdfeb3b126eeecb4399f11b50f4c428b1bee47"
1084
-  integrity sha512-pDqv++tRgrbRTA4De/BJ1YqRExbTBpeJhxiKCLf92I6Wrr4uqlOTQA60wW59iXMOpzO/y64RlRkE3oNkrI/CkA==
1083
+  resolved "git+https://github.com/kappa-db/multifeed#919e790543a4106b495f9aa9dd909b22fb87938f"
1085 1084
   dependencies:
1086 1085
     debug "^4.1.0"
1087 1086
     hypercore "^8.3.0"