Skip to content

Commit

Permalink
fix: tests fails because of a buffer related issue (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Dec 15, 2020
1 parent 9ff8607 commit 0a02810
Show file tree
Hide file tree
Showing 42 changed files with 1,019 additions and 297 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/pull-request-integration-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ on:
jobs:
test:
if: github.event.pull_request.draft == false
name: 'Run linter and tests'
name: 'Run example tests'
runs-on: ubuntu-latest
services:
nats:
image: nats
ports:
- 4222
restart: unless-stopped
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand All @@ -24,9 +18,19 @@ jobs:
node-version: 13
- name: Install dependencies
run: npm ci
- name: Generate and build examples
run: npm run test:examples:integration
env:
NATS_HOST: nats
NATS_PORT: ${{ job.services.redis.ports['6379'] }}
- name: Build the docker-compose stack
run: cd examples && docker-compose up -d
- name: Check running containers
run: docker ps -a
- name: Build pubsub example and test clients can talk to each other
run: |
npm run generate:examples:pubSub
npm run test:examples:integration:pubSub
- name: Build request/reply example and test clients can talk to each other
run: |
npm run generate:examples:requestReply
npm run test:examples:integration:requestReply
- name: Dump docker logs on failure
if: failure()
uses: jwalton/gh-docker-logs@v1

2 changes: 2 additions & 0 deletions .github/workflows/pull-request-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
run: npm ci
- name: Run linter
run: npm run lint
- name: Re-generate examples
run: npm run generate:examples
- name: Run tests
run: npm test

2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Run tests
run: npm test
- name: Re-generate examples
run: npm generate:examples
run: npm run generate:examples
- name: Get version from package.json before release step
id: initversion
run: echo "::set-output name=version::$(npm run get-version --silent)"
Expand Down
2 changes: 2 additions & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ services:
ports:
- 4222:4222
restart: unless-stopped
volumes:
- ./nats.conf:/nats-server.conf
1 change: 1 addition & 0 deletions examples/nats.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trace: true
2 changes: 1 addition & 1 deletion examples/publish subscribe/GenerateAndBuild.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

ag --install --output "./streetlight" "./streetlight.json" "../../" --force-write --param "generateTestClient=true" --param "promisifyReplyCallback=true" && cd ./streetlight && npm i && npm run build && cd ..
ag --output "./streetlight" "./streetlight.json" "../../" --force-write --param "generateTestClient=true" --param "promisifyReplyCallback=true" && cd ./streetlight && npm i && npm run build && cd ..
2 changes: 1 addition & 1 deletion examples/publish subscribe/streetlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"watch": "tsc --watch",
"build": "tsc",
"test:integration": "./node_modules/.bin/mocha -r ts-node/register src/tests/**/*.spec.ts"
"test:integration": "./node_modules/.bin/mocha -r ts-node/register src/tests/**/*.spec.ts --exit --timeout 10000"
},
"dependencies": {
"@types/klaw-sync": "^6.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function subscribe(
let subscribeOptions: SubscriptionOptions = {... options};

try{
let subscription = nc.subscribe(`streetlight.${streetlight_id}.command.turnon`, (err, msg) => {
let subscription = await nc.subscribe(`streetlight.${streetlight_id}.command.turnon`, (err, msg) => {
if(err){
onDataCallback(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, err));
}else{
Expand All @@ -33,7 +33,8 @@ channel = channel.substring(splits[0].length);
var streetlightIdEnd = channel.indexOf(splits[1]);
var streetlightIdParam = "" + channel.substring(0, streetlightIdEnd);


try{

try {
let receivedDataHooks = Hooks.getInstance().getreceivedDataHook();
var receivedData : any = msg.data;
Expand All @@ -42,10 +43,13 @@ try {
}
} catch (e) {
const error = NatsTypescriptTemplateError.errorForCode(ErrorCode.HOOK_ERROR, e);
reject(error);
return;
throw error;
}

}catch(e){
onDataCallback(e)
return;
}
onDataCallback(undefined, receivedData,
streetlightIdParam);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ export function publish(
return new Promise<void>(async (resolve, reject) => {
try{

try{
try {
let beforeSendingHooks = Hooks.getInstance().getBeforeSendingDataHook();
var dataToSend : any = message;
for(let hook of beforeSendingHooks){
dataToSend = hook(dataToSend);
}
}catch(e){
} catch(e){
const error = NatsTypescriptTemplateError.errorForCode(ErrorCode.HOOK_ERROR, e);
reject(error);
return;
throw error;
}

await nc.publish(`streetlight.${streetlight_id}.event.turnon`, dataToSend);
Expand Down
42 changes: 28 additions & 14 deletions examples/publish subscribe/streetlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export class NatsAsyncApiClient extends events.EventEmitter{
}
return false;
}

/**
* Disconnect all clients from the server
*/
async disconnect(){
if(this.jsonClient && !this.jsonClient!.isClosed()){
this.jsonClient!.close();
if(!this.isClosed()){
await this.jsonClient!.drain();
}
}

Expand Down Expand Up @@ -219,20 +219,34 @@ export class NatsAsyncApiClient extends events.EventEmitter{

,streetlight_id: string
,
flush?: boolean,
options?: SubscriptionOptions
): Promise<Subscription> {
return new Promise(async (resolve, reject) => {
const nc: Client = this.jsonClient!;
if(nc){
return streetlightStreetlightIdCommandTurnonChannel.subscribe(
onDataCallback, nc

,streetlight_id
,
options
);
}else{
return Promise.reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
if (nc) {
try {
const sub = await streetlightStreetlightIdCommandTurnonChannel.subscribe(
onDataCallback, nc

,streetlight_id
,
options
);
if(flush){
this.jsonClient!.flush(() => {
resolve(sub);
});
}else{
resolve(sub);
}
} catch (e) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ describe('streetlight/{streetlight_id}/command/turnon can talk to it self', () =
const natsUrl = `${natsHost}:${natsPort}`
await client.connectToHost(natsUrl);
await testClient.connectToHost(natsUrl);
});

it('Clients can connect', () => {
expect(client.isClosed()).to.be.false;
expect(testClient.isClosed()).to.be.false;
});
});

it('can send message', async () => {

Expand All @@ -37,11 +32,6 @@ var publishMessage: Client.TurnonCommandMessage.TurnonCommand = {
};
var StreetlightIdToSend: string = "string"


const tryAndWaitForResponse = new Promise(resolve => {
setTimeout(resolve, 100);
});

const replySubscription = await client.subscribeToStreetlightStreetlightIdCommandTurnon((err, msg

,streetlight_id
Expand All @@ -52,8 +42,23 @@ const replySubscription = await client.subscribeToStreetlightStreetlightIdComman

}
, StreetlightIdToSend

,
true
);
const tryAndWaitForResponse = new Promise((resolve, reject) => {
let isReturned = false;
setTimeout(() => {
if(!isReturned){
reject(new Error("Timeout"));
}
}, 3000)
setInterval(async () => {
if(replySubscription.getReceived() === 1){
resolve();
isReturned = true
}
}, 100);
});
await testClient.publishToStreetlightStreetlightIdCommandTurnon(publishMessage
, StreetlightIdToSend
);
Expand All @@ -65,11 +70,9 @@ expect(recievedStreetlightId).to.be.equal(StreetlightIdToSend);

});

it('Can shutdown', async () => {
await client.disconnect()
await testClient.disconnect()
expect(client.isClosed()).to.be.true;
expect(testClient.isClosed()).to.be.true;
after( async () => {
await client.disconnect();
await testClient.disconnect();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ describe('streetlight/{streetlight_id}/event/turnon can talk to it self', () =>
const natsUrl = `${natsHost}:${natsPort}`
await client.connectToHost(natsUrl);
await testClient.connectToHost(natsUrl);
});

it('Clients can connect', () => {
expect(client.isClosed()).to.be.false;
expect(testClient.isClosed()).to.be.false;
});
});

it('can send message', async () => {

Expand All @@ -37,12 +32,6 @@ var publishMessage: TestClient.AnonymousMessage2Message.AnonymousMessage2 = {
};
var StreetlightIdToSend: string = "string"


const tryAndWaitForResponse = new Promise(resolve => {
setTimeout(resolve, 100);
});


const replySubscription = await testClient.subscribeToStreetlightStreetlightIdEventTurnon((err, msg

,streetlight_id
Expand All @@ -53,8 +42,23 @@ const replySubscription = await testClient.subscribeToStreetlightStreetlightIdEv

}
, StreetlightIdToSend

,
true
);
const tryAndWaitForResponse = new Promise((resolve, reject) => {
let isReturned = false;
setTimeout(() => {
if(!isReturned){
reject(new Error("Timeout"));
}
}, 3000)
setInterval(async () => {
if(replySubscription.getReceived() === 1){
resolve();
isReturned = true
}
}, 100);
});
await client.publishToStreetlightStreetlightIdEventTurnon(publishMessage
, StreetlightIdToSend
);
Expand All @@ -66,11 +70,9 @@ expect(recievedStreetlightId).to.be.equal(StreetlightIdToSend);

});

it('Can shutdown', async () => {
await client.disconnect()
await testClient.disconnect()
expect(client.isClosed()).to.be.true;
expect(testClient.isClosed()).to.be.true;
after( async () => {
await client.disconnect();
await testClient.disconnect();
});

});

0 comments on commit 0a02810

Please sign in to comment.