Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callback to pubsub.unsubscribe and test (#300) #302

Closed
wants to merge 8 commits into from

Conversation

noot
Copy link
Contributor

@noot noot commented Dec 20, 2018

As per the issue I opened: #300

Upon pubsub.unsubscribe, the node was successfully unsubscribing, however the callback was never being called, so there was no explicit way to confirm success.

I have changed unsubscribe as to call the callback, so it now complies with the interface-ipfs-core spec. I have also added a test.

Let me know if any changes are needed, thanks!

@noot
Copy link
Contributor Author

noot commented Dec 20, 2018

Not sure how to fix the remaining failing tests, sorry.

Copy link
Contributor

@jacobheun jacobheun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about the commitlint ci failure, I can resolve that during the merge. We use the commits to generate changelogs so they are prefixed with certain values to denote what should be updated in the changelog, such as feat:, fix:, test:. Added a few notes on changes.

src/pubsub.js Outdated
@@ -43,6 +43,8 @@ module.exports = (node) => {
if (floodSub.listenerCount(topic) === 0) {
floodSub.unsubscribe(topic)
}

setImmediate(() => callback())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the callback is being added, even though it should have already exists, we should check that it is actually a function before invoking it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, I added a check

expect(err).to.not.exist()
console.log('tunsubscribed!')
done()
}), 600)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get rid of the timeouts in this test. Subscribing -> publishing -> unsubscribing should be able to be executed in serial successfully without a timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

nodes[0].pubsub.unsubscribe('pubsub', handler, (err) => {
expect(err).to.not.exist()
console.log('\tunsubscribed!')
done()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible done could be called before we run the expect in the publish handler. It would be good to include chai-checkmark in this file and mark each of the expects. That way we can do expect(4).checks(done) so we can ensure everything is verified before finishing the test.

Remove the console.log as well and I think this is good to go. Thank you for the PR!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I've added chai-checkmark and the check to make sure all the expects happen, as well as moved done and got rid of the console.log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done() is still being called here, so the test is finishing before expect check happens. Should just need to delete done on line 72.

Copy link
Contributor Author

@noot noot Dec 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I remove done() at line 72, I get Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. If we place done() at line 77, at the end of the waterfall, it works. I'm not sure if this correctly allows for the expect check to happen though.

@noot
Copy link
Contributor Author

noot commented Jan 3, 2019

Hey, any updates on this?

@jacobheun
Copy link
Contributor

Sorry for the delay, I've been away on holiday but am back now. The test done logic is still not happening correctly. Done is being called and is passed as a callback to waterfall, this isn't correct. The timeout you were hitting is probably because things aren't firing in the right order, or are being triggered early. I can take a deeper look at what the issue is later today.

@noot
Copy link
Contributor Author

noot commented Jan 3, 2019

@jacobheun No worries! Sounds good, thanks. Let me know what issues you find.

@jacobheun
Copy link
Contributor

There were some errors in the test, primarily some of the function arguments and .mark() not being added for chai-checkmark. The delay after the subscribe is also important, otherwise publish will likely be called before the peer is aware of the subscriber.

I think it was a bit hard to follow so I made some changes along with the fixes that hopefully make things a bit easier to read.

    it('start two nodes and send one message, then unsubscribe', (done) => {
      // Check the final series error, and the publish handler
      expect(2).checks(done)

      let nodes
      const data = Buffer.from('test')
      const handler = (msg) => {
        // verify the data is correct and mark the expect
        expect(msg.data).to.eql(data).mark()
      }

      series([
        // Start the nodes
        (cb) => startTwo((err, _nodes) => {
          nodes = _nodes
          cb(err)
        }),
        // subscribe on the first
        (cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
        // Wait a moment before publishing
        (cb) => setTimeout(cb, 500),
        // publish on the second
        (cb) => nodes[1].pubsub.publish('pubsub', data, cb),
        // unsubscribe on the first
        (cb) => nodes[0].pubsub.unsubscribe('pubsub', handler, cb),
        // Stop both nodes
        (cb) => stopTwo(nodes, cb)
      ], (err) => {
        // Verify there was no error, and mark the expect
        expect(err).to.not.exist().mark()
      })
    })

If that looks good and makes sense to you can you push the update?

@noot
Copy link
Contributor Author

noot commented Jan 4, 2019

@jacobheun That's a lot easier to follow, I think it makes sense. I'll push the changes now

@jacobheun
Copy link
Contributor

I'm going to look into the windows ci failure. It's possible it's just flakey, but it needs to get fixed either way.

@noot
Copy link
Contributor Author

noot commented Jan 5, 2019

okay cool, let me know if I need to make any changes.

@noot
Copy link
Contributor Author

noot commented Jan 19, 2019

@jacobheun hey, any updates or changes needed?

@jacobheun
Copy link
Contributor

@noot not yet, I was away last week, hoping to take a look at it this week.

// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noot sorry for the delay, it looks like the tests were just failing because windows needs some time before unsubscribing. This should fix it:

Suggested change
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the changes!

@jacobheun
Copy link
Contributor

@noot thank you for the PR! I've squashed and merged this to master via 679d446 to fix the code and commitlint.

@jacobheun jacobheun closed this Feb 1, 2019
maschad added a commit to maschad/js-libp2p that referenced this pull request Jun 21, 2023
Co-authored-by: Alex Potsides <alex@achingbrain.net>
maschad pushed a commit to maschad/js-libp2p that referenced this pull request Jun 21, 2023
## [1.0.13](libp2p/js-libp2p-crypto@v1.0.12...v1.0.13) (2023-03-10)

### Trivial Changes

* Update .github/workflows/semantic-pull-request.yml [skip ci] ([b66007c](libp2p/js-libp2p-crypto@b66007c))
* Update .github/workflows/semantic-pull-request.yml [skip ci] ([110063c](libp2p/js-libp2p-crypto@110063c))
* Update .github/workflows/semantic-pull-request.yml [skip ci] ([c789b0c](libp2p/js-libp2p-crypto@c789b0c))

### Dependencies

* **dev:** upgrade aegir to `38.1.2` ([libp2p#302](libp2p/js-libp2p-crypto#302)) ([9d60e39](libp2p/js-libp2p-crypto@9d60e39))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants