Skip to content

Commit

Permalink
bidirectional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner committed Sep 22, 2023
1 parent 518b0d1 commit 49d4623
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 134 deletions.
91 changes: 0 additions & 91 deletions tests/bidirectional/bidirectional.test.ts

This file was deleted.

10 changes: 9 additions & 1 deletion tests/bridge/can.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {createBridgeTest} from './utils'
import {createBidirectionalBridgeTest, createBridgeTest} from './utils'

describe('bridge', () => {
createBidirectionalBridgeTest(
'can',
{source: 'can', sourceName: 'can2xBridge'},
{target: 'can', targetName: 'can2xBridge'}
)
})

describe('bridge', () => {
createBridgeTest('can', {source: 'can'}, {target: 'can'})
Expand Down
16 changes: 15 additions & 1 deletion tests/bridge/mqtt.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import {createBridgeTest} from './utils'
import {createBidirectionalBridgeTest, createBridgeTest} from './utils'

// TODO: why does this not work
describe.skip('bridge', () => {
createBidirectionalBridgeTest(
'mqtt',
{
source: 'mqtt',
},
{
target: 'mqtt',
targetEndpoint: 'mqtt://localhost:3000',
}
)
})

describe('bridge', () => {
createBridgeTest(
Expand Down
16 changes: 15 additions & 1 deletion tests/bridge/socketio.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import {createBridgeTest} from './utils'
import {createBidirectionalBridgeTest, createBridgeTest} from './utils'

// TODO: make this work
describe.skip('bridge', () => {
createBidirectionalBridgeTest(
'socketio',
{
source: 'socketio',
},
{
target: 'socketio',
targetEndpoint: 'http://localhost:3000',
}
)
})

describe('bridge', () => {
createBridgeTest(
Expand Down
103 changes: 97 additions & 6 deletions tests/bridge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {BridgeSourceOptions, BridgeTargetOptions} from '#/actions/start-bridge'
import actions from '#actions'
import Message from '#core/message'
import Message, {CANMessage} from '#core/message'
import * as files from '#files'
import std from '#std'
import * as utils from '#utils'
import hae from '#utils/hae'
import {expect} from 'chai'
import {afterEach} from 'mocha'
import * as can from 'socketcan'

export function createBridgeTest(
name: string,
Expand All @@ -25,7 +26,7 @@ export function createBridgeTest(
const output = files.temporary()

// Start can source with file target
const receiver = await actions.bridge.start({
const bridge = await actions.bridge.start({
...bridgeSourceOptions,
target: 'file',
targetFile: output,
Expand All @@ -45,13 +46,13 @@ export function createBridgeTest(
await utils.sleep(25)

const result = files.loadFile(output).trim()
std.log({result: result})
std.log({expected: message.toString()})
expect(result).to.equal(message.toString())
const expected = message.toString()
std.log({result, expected})
expect(result).to.equal(expected)

await files.deleteFile(output)
await sender.stop()
await receiver.stop()
await bridge.stop()
})

afterEach(async () => {
Expand All @@ -61,3 +62,93 @@ export function createBridgeTest(
})
})
}

/**
* sender --(can2xSender)--> source <--(can2xBridge)--> target <--(can2xReceiver)--> receiver
* logger <------┘
*/
export function createBidirectionalBridgeTest(
name: string,
bridgeSourceOptions: BridgeSourceOptions,
bridgeTargetOptions: BridgeTargetOptions
) {
return describe(name, () => {
const cans = ['can2xSender', 'can2xReceiver', 'can2xBridge']

beforeEach(async () => {
for (const can of cans) {
await hae.try(async () => {
await actions.vcan.start({name: can})
}, `problem when creating vcan "${can}"`)
}
})

it(name, async () => {
const request = Message.fromJSON({id: 69, data: [1, 2, 3], ext: false, rtr: false})
const response = Message.fromJSON({id: 42, data: [4, 5, 6], ext: false, rtr: false})
const output = files.temporary()

// Receives "request" and returns "answer"
const receiver = can.createRawChannel(cans[1])
receiver.addListener('onMessage', function (message: CANMessage) {
std.log('receiver received', {message})
expect(Message.fromCAN(message).toString()).to.equal(request.toString())
receiver.send(response.toCAN())
})
receiver.start()

const target = await actions.bridge.start({
...bridgeSourceOptions,
target: 'can',
targetName: cans[1],
})

const source = await actions.bridge.start({
source: 'can',
sourceName: cans[0],
...bridgeTargetOptions,
})

// Send message using console source and can target
const logger = await actions.bridge.start({
source: 'can',
sourceName: cans[0],
target: 'file',
targetFile: output,
})

// Send message using console source and can target
const sender = await actions.bridge.start({
source: 'console',
sourceId: String(request.id),
sourceData: request.data.map(String),
sourceExt: request.ext,
sourceRtr: request.rtr,
target: 'can',
targetName: cans[0],
})

std.log('waiting for message being bridged')
await utils.sleep(250)

const result = files.loadFile(output).trim().split('\n')
const expected = [request.toString(), response.toString()]
std.log({result, expected})
expect(result).to.deep.equal(expected)

await files.deleteFile(output)
await sender.stop()
await logger.stop()
await target.stop()
await source.stop()
})

afterEach(async () => {
for (const can of cans) {
await hae.try(async () => {
await actions.vcan.stop({name: can})
}, `problem when stopping vcan "${can}"`)
}
})
})
}
16 changes: 15 additions & 1 deletion tests/bridge/ws.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import {createBridgeTest} from './utils'
import {createBidirectionalBridgeTest, createBridgeTest} from './utils'

// TODO: why does this not work
describe.skip('bridge', () => {
createBidirectionalBridgeTest(
'websocket',
{
source: 'ws',
},
{
target: 'ws',
targetEndpoint: 'ws://localhost:3000',
}
)
})

describe('bridge', () => {
createBridgeTest(
Expand Down
49 changes: 16 additions & 33 deletions tests/complex/complex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,44 @@ import Message from '#core/message'
import * as files from '#files'
import std from '#std'
import * as utils from '#utils'
import hae from '#utils/hae'
import {expect} from 'chai'
import {afterEach} from 'mocha'

/**
* console2can --(can2x1)--> can2socketio -> socketio2can --(can2x2)--> can2file
*/
describe('complex', () => {
beforeEach(async () => {
try {
await actions.vcan.start({
name: 'can2x1',
})
} catch (error) {
std.log('vcan "can2x1" not created', {error})
}
const cans = ['can2x1', 'can2x2']

try {
await actions.vcan.start({
name: 'can2x2',
})
} catch (error) {
std.log('vcan "can2x2" not created', {error})
beforeEach(async () => {
for (const can of cans) {
await hae.try(async () => {
await actions.vcan.start({name: can})
}, `problem when creating vcan "${can}"`)
}
})

it('source-target', async () => {
it('complex', async () => {
const message = Message.fromJSON({id: 69, data: [1, 2, 3], ext: false, rtr: false})
const output = files.temporary()

const can2file = await actions.bridge.start({
source: 'can',
sourceName: 'can2x2',
sourceName: cans[1],
target: 'file',
targetFile: output,
})

const socketio2can = await actions.bridge.start({
source: 'socketio',
target: 'can',
targetName: 'can2x2',
targetName: cans[1],
})

const can2socketio = await actions.bridge.start({
source: 'can',
sourceName: 'can2x1',
sourceName: cans[0],
target: 'socketio',
targetEndpoint: 'http://localhost:3000',
})
Expand All @@ -59,7 +52,7 @@ describe('complex', () => {
sourceExt: message.ext,
sourceRtr: message.rtr,
target: 'can',
targetName: 'can2x1',
targetName: cans[0],
})

std.log('waiting for message being bridged')
Expand All @@ -75,20 +68,10 @@ describe('complex', () => {
})

afterEach(async () => {
try {
await actions.vcan.stop({
name: 'can2x1',
})
} catch (error) {
std.log('vcan "can2x1" not stopped', {error})
}

try {
await actions.vcan.stop({
name: 'can2x2',
})
} catch (error) {
std.log('vcan "can2x" not stopped', {error})
for (const can of cans) {
await hae.try(async () => {
await actions.vcan.stop({name: can})
}, `problem when stopping vcan "${can}"`)
}
})
})

0 comments on commit 49d4623

Please sign in to comment.