Unverified Commit 26a724dc authored by Dan Jones's avatar Dan Jones
Browse files

test: retry behaviour for 403s

parent 341e40d0
......@@ -49,7 +49,7 @@
"axios": "^1.2.3",
"axios-mock-adapter": "^1.21.2",
"babel-jest": "^27.4.4",
"backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98",
"backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a",
"cross-env": "^7.0.3",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.3.0",
......
......@@ -14,6 +14,7 @@ const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = fixtures.get('schema-swagger');
const { GenericProtocol } = require('../../../dist/protocol');
const { Adapter } = require('../../../dist/adapter');
/**
* Use assert.CallTracker to track internal method calls
......@@ -27,11 +28,41 @@ const trackedFunction = function(method, params) {
};
const recorder = tracker.calls(trackedFunction);
class TrackedAdapter extends Adapter {
setupCallTracking(recorder, tracker) {
this.recorder = recorder;
this.tracker = tracker;
}
getAuthorizationHeader() {
this.recorder('getAuthorizationHeader');
return super.getAuthorizationHeader();
}
poll(is_retry) {
this.recorder('poll', { is_retry });
return super.poll(is_retry);
}
publish(topic, body, is_retry) {
this.recorder('publish', { topic, body, is_retry });
return super.publish(topic, body, is_retry);
}
broadcast(body, is_retry) {
this.recorder('broadcast', { body, is_retry });
return super.broadcast(body, is_retry);
}
getTrackedCalls(method) {
let calls = this.tracker.getCalls(this.recorder);
let methodCalls = calls.filter(call => call.arguments[0] === method);
return methodCalls;
}
resetTracker() {
this.tracker.reset();
}
}
class TrackedGenericProtocol extends GenericProtocol {
constructor(schema, services) {
super(schema, services);
this.recorder = services.recorder;
this.tracker = services.tracker;
setupCallTracking(recorder, tracker) {
this.recorder = recorder;
this.tracker = tracker;
}
encode(type, message) {
this.recorder('encode', {type, message});
......@@ -63,7 +94,12 @@ Before(function() {
recorder,
tracker
};
this.protocol = new TrackedGenericProtocol(this.schema, services);
this.classes = {
TrackedAdapter,
TrackedGenericProtocol,
};
this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services);
this.protocol.setupCallTracking(this.recorder, this.tracker);
this.protocol.resetTracker();
this.mockAxios = mockAxios;
this.mockAxios.reset();
......
const { When } = require('@cucumber/cucumber');
const assert = require('assert');
const { When, Then } = require('@cucumber/cucumber');
const { fixtures } = require('../../fixtures/server');
......@@ -22,4 +23,24 @@ When('the broadcast method is called', function() {
this.message = message;
const body = JSON.stringify(message);
this.call = this.adapter.broadcast(body);
this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length;
});
When('the broadcast method is called with is_retry on', function() {
const message = fixtures.get('message-vehicle-status');
this.message = message;
const body = JSON.stringify(message);
this.call = this.adapter.broadcast(body, true);
this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length;
});
Then('the broadcast method was called with is_retry on', function() {
let broadcastCalls = this.adapter.getTrackedCalls('broadcast');
let lastCall = broadcastCalls[broadcastCalls.length-1];
assert.ok(lastCall.arguments[1].is_retry);
});
Then('the broadcast method is not called again', function() {
let newBroadcastCallCount = this.adapter.getTrackedCalls('broadcast').length;
assert.equal(this.broadcastCallCount, newBroadcastCallCount);
});
\ No newline at end of file
......@@ -6,7 +6,7 @@ const { fixtures } = require('../../fixtures/server');
const mockValidConfig = fixtures.get('config-valid');
const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = require('../../mock/swagger.json');
const { Adapter } = require('../../../dist/adapter');
// const { Adapter } = require('../../../dist/adapter');
Given('valid config', function() {
this.config = mockValidConfig
......@@ -18,8 +18,8 @@ Given('invalid config', function() {
});
When('the adapter instance is created', function() {
let mockAdapter = new Adapter(this.protocol, this.config);
this.adapter = mockAdapter;
this.adapter = new this.classes.TrackedAdapter(this.protocol, this.config);
this.adapter.setupCallTracking(this.recorder, this.tracker);
});
Then('a successful response is returned with status {int}', function(expectedStatus) {
......@@ -34,4 +34,8 @@ Then('an error response is returned with status {int}', function(expectedStatus)
.catch((error) => {
assert.equal(error.response.status, expectedStatus);
});
});
Then('the credentials are deleted', function() {
assert.equal(this.adapter.credentials, null);
});
\ No newline at end of file
......@@ -45,3 +45,19 @@ Then('the protocol {string} method is called {int} times', function(method, xInv
const decodes = this.protocol.getTrackedCalls(method);
assert.equal(decodes.length, xInvokes);
});
When('the poll method is called with is_retry on', function() {
this.call = this.adapter.poll(true);
this.pollCallCount = this.adapter.getTrackedCalls('poll').length;
});
Then('the poll method was called with is_retry on', function() {
let pollCalls = this.adapter.getTrackedCalls('poll');
let lastCall = pollCalls[pollCalls.length-1];
assert.ok(lastCall.arguments[1].is_retry);
});
Then('the poll method is not called again', function() {
let newPollCallCount = this.adapter.getTrackedCalls('poll').length;
assert.equal(this.pollCallCount, newPollCallCount);
});
......@@ -24,4 +24,25 @@ When('the publish method is called', function() {
const topic = message.metadata.destination;
const body = JSON.stringify(message);
this.call = this.adapter.publish(topic, body);
this.publishCallCount = this.adapter.getTrackedCalls('publish').length;
});
When('the publish method is called with is_retry on', function() {
const message = fixtures.get('message-vehicle-status');
this.message = message;
const topic = message.metadata.destination;
const body = JSON.stringify(message);
this.call = this.adapter.publish(topic, body, true);
this.publishCallCount = this.adapter.getTrackedCalls('publish').length;
});
Then('the publish method was called with is_retry on', function() {
let publishCalls = this.adapter.getTrackedCalls('publish');
let lastCall = publishCalls[publishCalls.length-1];
assert.ok(lastCall.arguments[1].is_retry);
});
Then('the publish method is not called again', function() {
let newPublishCallCount = this.adapter.getTrackedCalls('publish').length;
assert.equal(this.publishCallCount, newPublishCallCount);
});
\ No newline at end of file
......@@ -1318,9 +1318,9 @@ babel-preset-jest@^27.5.1:
babel-plugin-jest-hoist "^27.5.1"
babel-preset-current-node-syntax "^1.0.0"
"backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98":
"backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a":
version "0.0.1"
resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98e9020a04895f79f6af5fb1c979f62259"
resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691affa53b04ac65b5fe7dc90df8e0286867"
balanced-match@^1.0.0:
version "1.0.2"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment