Skip to content

Commit

Permalink
feat: user prev implementation of measurement key
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Dec 4, 2023
1 parent 569bdf1 commit 2d14c34
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lib/redis/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const recordResult: RecordResultScript = defineScript({
local data = KEYS[3]
local date = KEYS[4]
local key = 'gp:measurement:'..measurementId
local awaitingKey = 'gp:measurement:probes_awaiting:'..measurementId
local awaitingKey = key..':probes_awaiting'
local probesAwaiting = redis.call('GET', awaitingKey)
if not probesAwaiting then
Expand Down Expand Up @@ -97,7 +97,7 @@ export const markFinished: MarkFinishedScript = defineScript({
SCRIPT: `
local measurementId = KEYS[1]
local key = 'gp:measurement:'..measurementId
local awaitingKey = 'gp:measurement:probes_awaiting:'..measurementId
local awaitingKey = key..':probes_awaiting'
redis.call('HDEL', 'gp:in-progress', measurementId)
redis.call('DEL', awaitingKey)
Expand Down
8 changes: 7 additions & 1 deletion src/measurement/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { getDefaults } from './schema/utils.js';
const logger = scopedLogger('store');

export const getMeasurementKey = (id: string, suffix: string | undefined = undefined): string => {
return `gp:measurement:${suffix ? suffix + ':' : ''}${id}`;
let key = `gp:measurement:${id}`;

if (suffix) {
key += `:${suffix}`;
}

return key;
};

const substractObjects = (obj1: Record<string, unknown>, obj2: Record<string, unknown> = {}) => {
Expand Down
90 changes: 90 additions & 0 deletions test/tests/integration/measurement/create-measurement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,96 @@ describe('Create measurement', () => {
});
});

it('should create measurement with another measurement id location', async () => {
let measurementId;
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
})
.expect(202)
.expect((response) => {
measurementId = response.body.id;
});

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: measurementId,
})
.expect(202)
.expect((response) => {
expect(response.body.id).to.exist;
expect(response.header.location).to.exist;
expect(response.body.probesCount).to.equal(1);
expect(response).to.matchApiSchema();
});
});

it('should create measurement with measurement id created from measurement id', async () => {
let measurementId1;
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
})
.expect(202)
.expect((response) => {
measurementId1 = response.body.id;
});

let measurementId2;
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: measurementId1,
})
.expect(202)
.expect((response) => {
measurementId2 = response.body.id;
});


await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: measurementId2,
})
.expect(202)
.expect((response) => {
expect(response.body.id).to.exist;
expect(response.header.location).to.exist;
expect(response.body.probesCount).to.equal(1);
expect(response).to.matchApiSchema();
});
});

it('should respond with error if there is no requested measurement id', async () => {
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: 'nonExistingMeasurementId',
})
.expect(422)
.expect((response) => {
expect(response.body).to.deep.equal({
error: {
message: 'No suitable probes found.',
type: 'no_probes_found',
},
links: {
documentation: 'https://www.jsdelivr.com/docs/api.globalping.io#post-/v1/measurements',
},
});

expect(response).to.matchApiSchema();
});
});

describe('adopted probes', () => {
before(async () => {
await client(ADOPTED_PROBES_TABLE).insert({
Expand Down
2 changes: 1 addition & 1 deletion test/tests/unit/measurement/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('measurement store', () => {
expect(redisMock.hSet.callCount).to.equal(1);
expect(redisMock.hSet.args[0]).to.deep.equal([ 'gp:in-progress', 'measurementid', 1678000000000 ]);
expect(redisMock.set.callCount).to.equal(1);
expect(redisMock.set.args[0]).to.deep.equal([ 'gp:measurement:probes_awaiting:measurementid', 4, { EX: 35 }]);
expect(redisMock.set.args[0]).to.deep.equal([ 'gp:measurement:measurementid:probes_awaiting', 4, { EX: 35 }]);
expect(redisMock.json.set.callCount).to.equal(2);

expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:measurement:measurementid', '$', {
Expand Down

0 comments on commit 2d14c34

Please sign in to comment.