diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3008a83f..0c4e342f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: - uses: DeterminateSystems/magic-nix-cache-action@v11 - run: nix develop --command make crossbuild - crossbuild-mac: + test-and-crossbuild-mac: runs-on: macos-latest steps: - uses: actions/checkout@v4 @@ -35,5 +35,7 @@ jobs: with: github_access_token: ${{ secrets.GITHUB_TOKEN }} - uses: DeterminateSystems/magic-nix-cache-action@v11 + - run: nix develop --command make build + - run: nix develop --command make test - run: nix develop --command make crossbuild_mac - run: nix develop --command make crossbuild_mac_bundles diff --git a/test/cors.js b/test/cors.js index 7e835a24..444cbcf1 100644 --- a/test/cors.js +++ b/test/cors.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const { wait, getJSON, startDriver, connectWS } = require('./utils') +const { wait, getJSON, startDriver, connectWS, waitForEndpoint } = require('./utils') const expect = require('chai').expect const httpEndpoints = [ @@ -24,7 +24,7 @@ beforeEach(async () => { driver = startDriver(...permissibleOrigins.flatMap(origin => [ '--permissible-origin', origin ])).on('exit', (c) => { code = c }) - await wait(500) + await waitForEndpoint('http://127.0.0.1:8382/'); expect(code).to.be.equal(0) driver.removeAllListeners() }) diff --git a/test/flex/helpers.js b/test/flex/helpers.js index cf798fa8..0384f152 100644 --- a/test/flex/helpers.js +++ b/test/flex/helpers.js @@ -1,20 +1,5 @@ const { wait } = require("../utils"); -async function waitForEndpoint(url, maxAttempts = 5, delay = 100) { - let lastError = null; - for (let i = 0; i < maxAttempts; i++) { - try { - // note: not connecting to websocket to avoid messing with test state by having a client - const res = await fetch(url); - return; - } catch (e) { - lastError = e; - await wait(delay); - } - } - throw new Error(`Endpoint ${url} not available after ${maxAttempts} attempts, last error: ${e}`); -} - /** * Generate a single synthetic Flex serial data frame. * @@ -135,7 +120,6 @@ function splitBufferRandomly(buffer, minChunkSize = 1, maxChunkSize = 10) { } module.exports = { - waitForEndpoint, generateFlexSerialFrame, generateSensitronicsFrame, generateRandomSensitronicsFrame, diff --git a/test/flex/index.js b/test/flex/index.js index 0c55618f..87867272 100644 --- a/test/flex/index.js +++ b/test/flex/index.js @@ -1,9 +1,8 @@ -const { wait, startDriver, connectWS, expectEvent } = require("../utils"); +const { wait, startDriver, connectWS, expectEvent, waitForEndpoint } = require("../utils"); const expect = require("chai").expect; const VirtualDevice = require("./mock/VirtualDevice"); const path = require("path"); const { - waitForEndpoint, generateFlexSerialFrame, generateRandomSensitronicsFrame, splitBufferRandomly, diff --git a/test/general.js b/test/general.js index 899d403a..8130c64e 100644 --- a/test/general.js +++ b/test/general.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const { wait, getJSON, startDriver } = require('./utils') +const { wait, getJSON, startDriver, waitForEndpoint } = require('./utils') const expect = require('chai').expect var driver @@ -10,7 +10,7 @@ beforeEach(async () => { driver = startDriver().on('exit', (c) => { code = c }) - await wait(500) + await waitForEndpoint('http://127.0.0.1:8382'); expect(code).to.be.equal(0) driver.removeAllListeners() }) diff --git a/test/rfid/index.js b/test/rfid/index.js index 4a66055e..c0d24e6f 100644 --- a/test/rfid/index.js +++ b/test/rfid/index.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -const { wait, startDriver, connectWS, getJSON, expectEvent } = require('../utils') +const { wait, startDriver, connectWS, getJSON, expectEvent, waitForEndpoint } = require('../utils') const expect = require('chai').expect // TESTS @@ -14,8 +14,7 @@ describe('Basic functionality', () => { driver = startDriver().on('exit', (c) => { code = c }) - // Give driver 500ms to start up - await wait(500) + await waitForEndpoint('http://127.0.0.1:8382/rfid'); expect(code).to.be.equal(0) driver.removeAllListeners() }) diff --git a/test/senso/index.js b/test/senso/index.js index b0c864e1..62ee16b8 100644 --- a/test/senso/index.js +++ b/test/senso/index.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -const { wait, startDriver, connectWS, expectEvent } = require('../utils') +const { wait, startDriver, connectWS, expectEvent, waitForEndpoint, skipTestOnMacOSinCI } = require('../utils') const expect = require('chai').expect const mock = require('./mock') @@ -16,8 +16,7 @@ describe('Basic functionality', () => { driver = startDriver().on('exit', (c) => { code = c }) - // Give driver 500ms to start up - await wait(500) + await waitForEndpoint('http://127.0.0.1:8382/senso'); expect(code).to.be.equal(0) driver.removeAllListeners() @@ -213,6 +212,8 @@ describe('Basic functionality', () => { }) it('Can discover mock Senso', async function () { + // bonjour / mDNS does not seem to work on Github's macOS runners + skipTestOnMacOSinCI(this) this.timeout(6000) // connect with Senso WS diff --git a/test/utils.js b/test/utils.js index 7774cbaa..53b79c6c 100644 --- a/test/utils.js +++ b/test/utils.js @@ -48,5 +48,30 @@ module.exports = { } }) }) + }, + + // Checks whether a connection can be established via HTTP to URL, regardless + // of status code. + waitForEndpoint: async function(url, maxAttempts = 10, delay = 100) { + let lastError = null + for (let i = 0; i < maxAttempts; i++) { + try { + const res = await fetch(url) + return + } catch (e) { + lastError = e + await module.exports.wait(delay) + } + } + throw new Error(`Endpoint ${url} not available after ${maxAttempts} attempts, last error: ${e}`) + }, + + skipTestOnMacOSinCI: function (test) { + const isMac = process.platform === 'darwin' + const isGitHubActions = process.env.GITHUB_ACTIONS === 'true' + + if (isMac && isGitHubActions) { + test.skip() + } } }