Skip to content

Commit

Permalink
feat: bulk changes
Browse files Browse the repository at this point in the history
- working with node-hl7-server module
- clients always create OUTBOUND requests

#15

[ci skip]
  • Loading branch information
Bugs5382 committed Dec 10, 2023
1 parent 78a667b commit dd0a638
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 225 deletions.
116 changes: 32 additions & 84 deletions __tests__/hl7.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import portfinder from 'portfinder'
import {Server, Listener as ServerListener} from "../../node-hl7-server/src";
import {Client, Listener, Message} from '../src'
import {Hl7Inbound, Server} from "../../node-hl7-server/src";
import {Client, HL7Outbound, Message} from '../src'
import {expectEvent} from "./__utils__/utils";

describe('node hl7 client', () => {
Expand All @@ -10,55 +10,55 @@ describe('node hl7 client', () => {
test(`error - hostname has to be string`, async () => {
try {
// @ts-expect-error this is not a string
new Client({hostname: 351123})
new Client({host: 351123})
} catch (err: any) {
expect(err.message).toBe('hostname is not valid string.')
}
})

test(`error - ipv4 and ipv6 both can not be true exist`, async () => {
try {
new Client({hostname: '5.8.6.1', ipv6: true, ipv4: true})
new Client({host: '5.8.6.1', ipv6: true, ipv4: true})
} catch (err: any) {
expect(err.message).toBe('ipv4 and ipv6 both can\'t be set to be both used exclusively.')
}
})

test(`error - ipv4 not valid address`, async () => {
try {
new Client({hostname: "123.34.52.455", ipv4: true})
new Client({host: "123.34.52.455", ipv4: true})
} catch (err: any) {
expect(err.message).toBe('hostname is not a valid IPv4 address.')
}
})

test(`error - ipv4 valid address`, async () => {
try {
new Client({hostname: "123.34.52.45", ipv4: true})
new Client({host: "123.34.52.45", ipv4: true})
} catch (err: any) {
expect(err.message).toBeUndefined()
}
})

test(`error - ipv6 not valid address`, async () => {
try {
new Client({hostname: "2001:0db8:85a3:0000:zz00:8a2e:0370:7334", ipv6: true})
new Client({host: "2001:0db8:85a3:0000:zz00:8a2e:0370:7334", ipv6: true})
} catch (err: any) {
expect(err.message).toBe('hostname is not a valid IPv6 address.')
}
})

test(`error - ipv6 valid address`, async () => {
try {
new Client({hostname: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ipv6: true})
new Client({host: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ipv6: true})
} catch (err: any) {
expect(err.message).toBeUndefined()
}
})

test(`properties exist`, async () => {
const client = new Client({ hostname: 'hl7.server.com'})
expect(client).toHaveProperty("connectToListener")
const client = new Client({ host: 'hl7.server.com'})
expect(client).toHaveProperty("createOutbound")
})

})
Expand All @@ -68,14 +68,14 @@ describe('node hl7 client', () => {
let client: Client

beforeEach(() => {
client = new Client({hostname: 'localhost'})
client = new Client({host: 'localhost'})
})

test('error - no port specified', async () => {

try {
// @ts-expect-error port is not specified
client.connectToListener()
client.createOutbound()
} catch (err: any) {
expect(err.message).toBe('port is not defined.')
}
Expand All @@ -85,23 +85,23 @@ describe('node hl7 client', () => {
test('error - port not a number', async () => {
try {
// @ts-expect-error port is not specified as a number
client.connectToListener({ port: "12345"}, async () => {})
client.createOutbound({ port: "12345"}, async () => {})
} catch (err: any) {
expect(err.message).toBe('port is not valid number.')
}
})

test('error - port less than 0', async () => {
try {
client.connectToListener({ port: -1}, async () => {})
client.createOutbound({ port: -1}, async () => {})
} catch (err: any) {
expect(err.message).toBe('port must be a number (0, 65353).')
}
})

test('error - port greater than 65353', async () => {
try {
client.connectToListener({ port: 65354}, async () => {})
client.createOutbound({ port: 65354}, async () => {})
} catch (err: any) {
expect(err.message).toBe('port must be a number (0, 65353).')
}
Expand All @@ -119,10 +119,10 @@ describe('node hl7 client', () => {
})

const server = new Server({ bindAddress: 'localhost'})
const listener = server.createListener({port: LISTEN_PORT}, async () => {})
const listener = server.createInbound({port: LISTEN_PORT}, async () => {})

const client = new Client({ hostname: 'localhost'})
const outGoing = client.connectToListener({ port: LISTEN_PORT }, () => {})
const client = new Client({ host: 'localhost'})
const outGoing = client.createOutbound({ port: LISTEN_PORT }, async () => {})

await expectEvent(listener, 'client.connect')
await expectEvent(outGoing, 'connect')
Expand All @@ -132,77 +132,17 @@ describe('node hl7 client', () => {

})

test('...should be able to connect to the same port from different clients', async () => {

const LISTEN_PORT = await portfinder.getPortPromise({
port: 3000,
stopPort: 65353
})

const server = new Server({ bindAddress: 'localhost'})
const listener = server.createListener({port: LISTEN_PORT}, async () => {})

const client = new Client({ hostname: 'localhost'})
const outGoing = client.connectToListener({ port: LISTEN_PORT }, () => {})

const client_2 = new Client({ hostname: 'localhost'})
const outGoing_2 = client_2.connectToListener({ port: LISTEN_PORT }, () => {})

await expectEvent(outGoing, 'connect')
await expectEvent(outGoing_2, 'connect')

await outGoing.close()
await outGoing_2.close()
await listener.close()

})

test('...two different ports', async () => {

const LISTEN_PORT = await portfinder.getPortPromise({
port: 3000,
stopPort: 65353
})

const LISTEN_PORT_2 = await portfinder.getPortPromise({
port: 3001,
stopPort: 65353
})

const server = new Server({ bindAddress: 'localhost'})
const listener = server.createListener({port: LISTEN_PORT}, async () => {})

const server_2 = new Server({ bindAddress: 'localhost'})
const listener_2 = server_2.createListener({port: LISTEN_PORT_2}, async () => {})


const client = new Client({ hostname: 'localhost'})
const outGoing = client.connectToListener({ port: LISTEN_PORT }, () => {})

const client_2 = new Client({ hostname: 'localhost'})
const outGoing_2 = client_2.connectToListener({ port: LISTEN_PORT_2 }, () => {})

await expectEvent(outGoing, 'connect')
await expectEvent(outGoing_2, 'connect')

await outGoing.close()
await outGoing_2.close()
await listener.close()
await listener_2.close()

})

})

describe('end to end testing', () => {
describe('server/client sanity checks', () => {

let waitAck: number = 0

let server: Server
let listener: ServerListener
let listener: Hl7Inbound

let client: Client
let outGoing: Listener
let outGoing: HL7Outbound

beforeEach(async () => {

Expand All @@ -212,10 +152,10 @@ describe('node hl7 client', () => {
})

server = new Server({bindAddress: 'localhost'})
listener = server.createListener({port: LISTEN_PORT}, async () => {})
listener = server.createInbound({port: LISTEN_PORT}, async () => {})

client = new Client({hostname: 'localhost'})
outGoing = client.connectToListener({port: LISTEN_PORT, waitAck: waitAck !== 2}, async () => {})
client = new Client({host: 'localhost'})
outGoing = client.createOutbound({port: LISTEN_PORT, waitAck: waitAck !== 2}, async () => {})

})

Expand Down Expand Up @@ -295,4 +235,12 @@ describe('node hl7 client', () => {

})

describe('end to end tests', () => {

test.skip('...send message, get proper ACK', async () => {

})

})

})
11 changes: 8 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from 'events'
import { Listener } from './listener.js'
import { normalizeClientOptions, ClientListenerOptions, ClientOptions } from '../utils/normalizeClient.js'
import {HL7Outbound} from "./hl7Outbound";

/**
* Client Class
Expand All @@ -16,7 +16,12 @@ export class Client extends EventEmitter {

/** Connect to a listener to a specified port.
* @since 1.0.0 */
connectToListener (props: ClientListenerOptions, handler: any): Listener {
return new Listener(this, props, handler)
createOutbound (props: ClientListenerOptions, handler?: any): HL7Outbound {
return new HL7Outbound(this, props, handler)
}

getHost(): string {
return this._opt.host
}

}
Loading

0 comments on commit dd0a638

Please sign in to comment.