Skip to content

Commit

Permalink
PUB-93 - Add support for labelling on all resources (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri authored Sep 13, 2024
1 parent 8e85602 commit aa6ea15
Show file tree
Hide file tree
Showing 25 changed files with 1,585 additions and 980 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-geckos-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@honestfoodcompany/pubsub': patch
---

Fix topic options default retry config not being applied if custom options provided
5 changes: 5 additions & 0 deletions .changeset/wild-bees-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@honestfoodcompany/pubsub': minor
---

Set labels for subscriptions, dlq on update too and support for labels to topics - PUB-93
5 changes: 5 additions & 0 deletions .changeset/yellow-tables-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@honestfoodcompany/pubsub': patch
---

Update logging to be more structured - PUB-87
13 changes: 5 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@
"[javascript]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
}
},
"[javascriptreact]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
}
},
"[typescript]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
}
},
"[typescriptreact]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
}
},
"editor.tabCompletion": "on",
"editor.tabSize": 2,
"editor.rulers": [
80,
120
],
"editor.rulers": [80, 120],
"jest.jestCommandLine": "yarn test"
}
2 changes: 1 addition & 1 deletion __tests__/pubsub.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('@PubSub Service Init', () => {
});

it('should call init', () => {
expect(SubService.init).toBeCalled();
expect(SubService.init).toHaveBeenCalled();
});

it('should override closeAll method', () => {
Expand Down
41 changes: 19 additions & 22 deletions __tests__/subscription.with-custom-credentials.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PubSubService, SubscriptionService } from '@honestfoodcompany/pubsub';
import { mocked } from 'ts-jest/utils';
import { SubscriberTuple } from '../src/subscriber';
import GooglePubSubAdapter from '../src/client/googlePubSub';
import { createProject } from '../src/client/googlePubSub/project';
Expand All @@ -15,18 +14,7 @@ jest.mock('../src/client/googlePubSub/project', () => {
};
});

const mockPublish = jest.fn();
const mockSubscribe = jest.fn();
const mockGet = jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
publish: mockPublish,
},
]);
});
});

const mockConstructor = jest.fn();
const mockClose = jest.fn();
const mockRemoveListeners = jest.fn();
Expand All @@ -47,9 +35,18 @@ jest.mock('@google-cloud/pubsub', () => {
return {
subscription: mockSubscription,
subscribe: mockSubscribe(config),
topic: jest.fn(() => ({
get: mockGet,
getSubscriptions: jest.fn(() => ['dummySub']),
topic: jest.fn((topicName) => ({
get: jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
name: topicName,
publish: jest.fn(),
getSubscriptions: jest.fn(() => ['dummySub']),
},
]);
});
}),
})),
};
}),
Expand Down Expand Up @@ -91,9 +88,9 @@ describe('With Project Credentials', (): void => {
const getProject = jest.spyOn(GooglePubSubAdapter.prototype, 'getProject');
await PubSubService.getInstance().subscribe(subscription);

expect(subscribe).toBeCalled();
expect(getProject).toBeCalledWith(subscription[1].options);
expect(mocked(createProject).mock.calls[1]).toEqual([
expect(subscribe).toHaveBeenCalled();
expect(getProject).toHaveBeenCalledWith(subscription[1].options);
expect(jest.mocked(createProject).mock.calls[1]).toEqual([
'google-pubsub-project-id',
{
credentials: {
Expand All @@ -111,13 +108,13 @@ describe('With Project Credentials', (): void => {
projectId: 'google-pubsub-project-id',
},
]);
expect(mockSubscription).toBeCalledTimes(1);
expect(mockSubscription).toHaveBeenCalledTimes(1);
});

it('should close existing subscription and skip not subscribed topics', async (): Promise<void> => {
await PubSubService.getInstance().closeAll();
expect(mockSubscription).toBeCalledTimes(1);
expect(mockClose).toBeCalledTimes(1);
expect(mockRemoveListeners).toBeCalledTimes(1);
expect(mockSubscription).toHaveBeenCalledTimes(1);
expect(mockClose).toHaveBeenCalledTimes(1);
expect(mockRemoveListeners).toHaveBeenCalledTimes(1);
});
});
35 changes: 14 additions & 21 deletions __tests__/topic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PubSubService from '../src/service/pubsub';
import { defaultOptions } from '../src/topic';
import TestTopic from './pubsub/topics/test-topic';
import TestTopicNoTimeStamp from './pubsub/topics/test-topic.no.timestamp';

Expand All @@ -18,18 +19,7 @@ jest.mock('../src/service/pubsub', () => ({

describe('topics', (): void => {
const defaultRetrySettings = {
retryConfig: {
backoffSettings: {
initialRetryDelayMillis: 100,
initialRpcTimeoutMillis: 5000,
maxRetryDelayMillis: 60000,
maxRpcTimeoutMillis: 600000,
retryDelayMultiplier: 1.3,
rpcTimeoutMultiplier: 1,
totalTimeoutMillis: 600000,
},
retryCodes: [10, 1, 4, 13, 8, 14, 2],
},
retryConfig: defaultOptions.retryConfig,
};

beforeEach(() => {
Expand All @@ -40,25 +30,25 @@ describe('topics', (): void => {
const spy = jest.spyOn(PubSubService, 'getInstance');
const topic = new TestTopic();
await topic.publish({ data: 'test' });
expect(spy).toBeCalledTimes(1);
expect(mockPublish).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledTimes(1);
});

it('Expect publish to return a string with the messageId', async (): Promise<void> => {
const spy = jest.spyOn(PubSubService, 'getInstance');
const topic = new TestTopic();
const data = await topic.publish({ data: 'test' });
expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(data).toBe('testid');
expect(mockPublish).toBeCalledTimes(1);
expect(mockPublish).toHaveBeenCalledTimes(1);
});

it('Should test retry config to be the default one', async (): Promise<void> => {
const spy = jest.spyOn(PubSubService.prototype, 'publish');
const topic = new TestTopic();
const data = await topic.publish({ data: 'test' });
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.objectContaining({
_timestamp: expect.stringContaining(':'),
Expand All @@ -74,12 +64,15 @@ describe('topics', (): void => {
const topic = new TestTopicNoTimeStamp();
const data = await topic.publish({ data: 'test' });
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopicNoTimeStamp,
expect.not.objectContaining({
_timestamp: expect.stringContaining(':'),
}),
{},
expect.objectContaining({
...defaultOptions,
addTimeStamp: false,
}),
);
});

Expand All @@ -95,7 +88,7 @@ describe('topics', (): void => {
},
);
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.any(Object),
expect.objectContaining({
Expand Down Expand Up @@ -123,7 +116,7 @@ describe('topics', (): void => {
{ attributes: { test: 'filter' } },
);
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.any(Object),
expect.objectContaining({
Expand Down
30 changes: 15 additions & 15 deletions __tests__/topic.with-custom-credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import TestTopicWithProjectCredentials from './pubsub/topics/test-topic.with-cus
process.env.PUBSUB_DRIVER = 'google';

const mockPublish = jest.fn();
const mockGet = jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
publish: mockPublish,
publishJSON: mockPublish,
},
]);
});
});

const mockConstructor = jest.fn();
jest.mock('@google-cloud/pubsub', () => {
Expand All @@ -28,8 +18,18 @@ jest.mock('@google-cloud/pubsub', () => {
exists: jest.fn(() => true),
})),
subscribe: jest.fn(),
topic: jest.fn(() => ({
get: mockGet,
topic: jest.fn((topicName) => ({
get: jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
name: topicName,
publish: mockPublish,
publishJSON: mockPublish,
},
]);
});
}),
})),
};
}),
Expand All @@ -41,20 +41,20 @@ describe('With Custom Credentials', (): void => {
const spy = jest.spyOn(PubSubService.prototype, 'publish');
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(spy).toBeCalled();
expect(spy).toHaveBeenCalled();
});

it('should call Google Driver publish method', async (): Promise<void> => {
const spy = jest.spyOn(GooglePubSubAdapter.prototype, 'publish');
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(spy).toBeCalled();
expect(spy).toHaveBeenCalled();
});

it('should call GooglePubSub publish method', async (): Promise<void> => {
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(mockPublish).toBeCalled();
expect(mockPublish).toHaveBeenCalled();
});

it('should have the project defined in projects', async () => {
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ process.env.PUBSUB_ROOT_DIR = './__tests__/pubsub';
process.env.PUBSUB_HEALTH_SERVER = 'false';

const TEST_REGEX = '/__tests__/.*.test.(js|ts)?$';
/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
// FIXME: Automatically clear mock calls and instances between every test
// clearMocks: true,
Expand All @@ -13,6 +16,7 @@ module.exports = {
'src/**/*.{js,ts}',
'!**/*.{generated,interface,test,data,enums}.{ts}',
'!**/*.d.ts',
'!src/interface/**',
'!src/__tests__/**',
'!**/node_modules/**',
],
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export default {
Logger.Instance.warn(chalk.white.bold('\n No subscriptions found'));
} else {
const table = new Table({
head: ['Topic Name', 'Subscription Name', 'Description'],
head: ['Topic Name', 'Subscription Name', 'DLQ', 'Description'],
});
table.push(
...PubSubService.getInstance()
.getSubscribers()
.map((row: SubscriberTuple) => [
row[1].topicName,
row[1].subscriptionName,
row[1].options?.deadLetterPolicy?.deadLetterTopic ?? 'N/A',
wrapAnsi(row[1].description || '', 100),
]),
);
Expand Down
Loading

0 comments on commit aa6ea15

Please sign in to comment.