mafintosh / hyperdb

Distributed scalable database
MIT License
752 stars 75 forks source link

Replication in test to unauthorized peer fails when already replicating to authorized peer #101

Closed jimpick closed 6 years ago

jimpick commented 6 years ago

I encountered a problem using replicate() in a test.

tape('replication to two new peers, only authorize one writer', function (t) {
  var a = create.one()
  a.ready(function () {
    var b = create.one(a.key)
    b.ready(function () {
      var c = create.one(a.key)
      c.ready(function () {
        a.put('foo', 'bar', function (err) {
          t.error(err)
          a.authorize(b.local.key, function () {
            replicate(a, b, function () {
              t.error(err)
              console.log('The following replicate will fail')
              replicate(a, c, function () {
                c.authorized(c.local.key, function (err, auth) {
                  t.error(err)
                  t.equals(auth, false)
                  t.end()
                })
              })
            })
          })
        })
      })
    })
  })
})

Output:

$ npx tape test/auth2.js
TAP version 13
# replication to 2 new peers, only authorize one writer
ok 1 null
ok 2 null
The following replicate will fail
not ok 3 test exited without ending
  ---
    operator: fail
    at: process.<anonymous> (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/index.js:90:19)
    stack: |-
      Error: test exited without ending
          at Test.assert [as _assert] (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:225:54)
          at Test.bound [as _assert] (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:77:32)
          at Test.fail (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:318:10)
          at Test.bound [as fail] (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:77:32)
          at Test._exit (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:191:14)
          at Test.bound [as _exit] (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/lib/test.js:77:32)
          at process.<anonymous> (/Users/jim/backblend-jpimac/clients/research-team-tool/hyperdb-tests/hyperdb/node_modules/tape/index.js:90:19)
          at process.emit (events.js:159:13)
  ...

1..3
# tests 3
# pass  2
# fail  1

It appears the first .replicate to peer b will work, but the second .replicate to peer c exits the test without calling the callback.

If I authorize peer c before calling .replicate, then it will work as expected.

mafintosh commented 6 years ago

Confirmed bug. Added the test case here, https://github.com/mafintosh/hyperdb/commit/dc5107ff7d4b7551a38afc5f36409617d6b7bf82 and a fix in the following commit, https://github.com/mafintosh/hyperdb/commit/ef4400ca9c3fc97e6606bb647dde85630ec8fcbc