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

Fix a race condition in startNotification with the "valuechanged" event notifications #77

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nmasse-itix
Copy link

This Pull Request is a proposal to fix #72 by removing a race condition between startNotification and the "valuechanged" event notifications.

The unit test wraps the StartNotify call with an early event emission.
An assertion is positioned to ensure that this early event is not lost.

With the original code, the new unit test fails with:

$ npm run test:jest

> [email protected] test:jest
> jest --testPathIgnorePatterns=test-e2e/

 PASS  test/GattService.spec.js
 PASS  test/buildTypedValue.spec.js
 PASS  test/GattServer.spec.js
 PASS  test/parseDict.spec.js
 PASS  test/Device.spec.js
 PASS  test/Bluetooth.spec.js
 PASS  test/BusHelper.spec.js
 FAIL  test/GattCharacteristic.spec.js
  ● race condition between event:valuechanged / startNotification

    expect(received).toHaveLength(expected)

    Expected length: 2
    Received length: 1
    Received array:  [[{"data": [98, 97, 114], "type": "Buffer"}]]

      129 |
      130 |   // Check the mocked callback function has been called twice
    > 131 |   expect(cb.mock.calls).toHaveLength(2);
          |                         ^
      132 |   
      133 |   // Cleanup
      134 |   characteristic.off('valuechanged', cb)

      at Object.toHaveLength (test/GattCharacteristic.spec.js:131:25)

 PASS  test/Adapter.spec.js

Test Suites: 1 failed, 8 passed, 9 total
Tests:       1 failed, 37 passed, 38 total
Snapshots:   0 total
Time:        1.84 s, estimated 2 s
Ran all test suites.

With the proposed code, all tests are passing.

@nmasse-itix nmasse-itix changed the title see #72: fix a race condition in startNotification with the "valuechanged" event notifications Fix a race condition in startNotification with the "valuechanged" event notifications Nov 27, 2024
@coveralls
Copy link

Coverage Status

coverage: 96.247%. remained the same
when pulling 381cd1b on Demo-AI-Edge-Crazy-Train:issue-72
into f936585 on chrvadala:main.

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.

Race condition in GattCharacteristic.js / startNotifications()
2 participants