Closed joshuakarp closed 2 years ago
Multiple suggestions to fix this:
Uint8Array
, it's possible to apply bitwise operators to each byte element rather than doing char code increment e.g. nodeId[15] |= 0
KeyManager
and provide deterministic NodeIds instead, to do mocking review these resources:
Mock documentation is on jest, but because we are using TS, we need to ensure that TS doesn't complain about types. So you would want to mock the nodeid/rootkey that is being provided by the KeyManager.
But you should probably need to fix the node id operations, since char code increment is not correct anyway.
Consider changing the timeouts in the code when doing these tests too. Jest also supports timeout mocking that can help too: https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers
I've added a note that this would be blocked by #261. This is because the current NodeGraph
tests are failing on account of the node ID being used in the tests. Depending on the representation to be used in #261, this may have consequences for how this issue should be resolved (however, this is probably unlikely - the tests themselves are already using the 32-byte representation of the node ID). Basically, if there's a choice in which issue should be completed first, #261 should be completed first.
Regarding mocking from this earlier comment https://github.com/MatrixAI/js-polykey/issues/274#issuecomment-952583901, can look into the following for mocking only a single function: https://stackoverflow.com/questions/59312671/mock-only-one-function-from-module-but-leave-rest-with-original-functionality (originally brought up in https://github.com/MatrixAI/js-polykey/pull/283#issuecomment-981569311)
There's some discussion of function mocking in #283:
Seems that mocking with a static keypair has also created problems for the nodes tests, as per https://github.com/MatrixAI/js-polykey/pull/283#issuecomment-982374649. Off the top of my head, not too sure what could be causing this - I thought I was mutating node IDs directly, as opposed to creating new keypairs. But will investigate this when looking into this issue too.
I should also add an exception thrown for when we attempt to calculate the bucket index with a target node ID that matches the current node ID. This currently (albeit, incorrectly) returns -1 for the bucket index, which causes some hard-to-trace problems when debugging (most recently here https://github.com/MatrixAI/js-polykey/pull/278#issuecomment-985212757).
Alternatively, find the calls to this function, and prevent this calculation from occurring at all.
What was the resolution to this issue? @joshuakarp @tegefaulkes can you describe how #310 solved this?
The problems was caused by incrementNodeId
creating a nodeId that fell outside of the expected bucket randomly. I removed it and updated generateNodeIdForBucket
to take an alt
number to make an alternative NodeId
that will always fit within the expected bucket.
Describe the bug
The tests in
NodeGraph
can intermittently fail across runs. We'd experienced some similar failures in the past, and I thought I'd resolved them. The past failures were to do with some test utility functions that generate node IDs for a specific bucket index, such that they can be used for dynamic testing. They were previously causing failures by generating node IDs that overflowed into a different bucket index.If this is still the cause of failure, now that we can use the 32 byte array as a node ID, I can potentially make these test utilities a bit more robust. Previously, they were just incrementing a character in the node ID to force an expected XOR value.
To Reproduce
The test failures only occur randomly. It took me 5 runs of
NodeGraph.test.ts
to experience a failure in the tests. Some instances of these:I've found it useful to use the
!#
in the terminal to repeat the command (repeats everything prior). e.g.npm test -- tests/nodes/NodeGraph.test.ts; !# !#
to run the tests 4 times.Additional context
vaultsrefactoring
MR aboutNodeGraph
test failures: https://gitlab.com/MatrixAI/Engineering/Polykey/js-polykey/-/merge_requests/205#note_695753874