Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for debounce by specific fields #4

Merged
merged 14 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ yarn-error.log
.DS_Store
.settings.json
/bazel-*
.ijwb
.ijwb
*.log
2 changes: 2 additions & 0 deletions src/main/java/configuration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Config {
//Required
public static String KAFKA_BROKER;
public static String GROUP_ID;
public static String COMMIT_INTERVAL_MS_CONFIG;
public static String SOURCE_TOPIC;
public static String TARGET_TOPIC;

Expand Down Expand Up @@ -43,6 +44,7 @@ public static void init() throws Exception {

KAFKA_BROKER = getString(dotenv, "KAFKA_BROKER");
GROUP_ID = getString(dotenv, "GROUP_ID");
COMMIT_INTERVAL_MS_CONFIG = getOptionalString(dotenv, "COMMIT_INTERVAL_MS_CONFIG", "30000");

// --------------------
SOURCE_TOPIC = getString(dotenv, "SOURCE_TOPIC");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/stream/StreamConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class StreamConfiguration extends Properties {
public StreamConfiguration() {
super();
put(StreamsConfig.APPLICATION_ID_CONFIG, Config.GROUP_ID);
put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, Config.COMMIT_INTERVAL_MS_CONFIG);
put(StreamsConfig.CLIENT_ID_CONFIG, Config.GROUP_ID + "_client");
put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, Config.KAFKA_BROKER);
put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
Expand Down
2 changes: 2 additions & 0 deletions tests/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const config: JestConfigWithTsJest = {
// eslint-disable-next-line @typescript-eslint/naming-convention
'^(\\.{1,2}/.*)\\.js$': '$1',
},
// setupFilesAfterEnv: ['<rootDir>/setupFilesAfterEnv.ts'],
testTimeout: 1800000,
};

export default config;
17 changes: 17 additions & 0 deletions tests/services/consume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Kafka, KafkaMessage} from 'kafkajs';

export const consume = async (kafka: Kafka, topic: string, parse = true) => {
const consumer = kafka.consumer({groupId: 'orchestrator'});
await consumer.subscribe({topic: topic, fromBeginning: true});
const consumedMessage = await new Promise<KafkaMessage>((resolve) => {
consumer.run({
eachMessage: async ({message}) => resolve(message),
});
});
await consumer.disconnect();
const value = parse ? JSON.parse(consumedMessage.value?.toString() ?? '{}') : consumedMessage.value?.toString();
const headers = Object.fromEntries(
Object.entries(consumedMessage.headers!).map(([key, value]) => [key, value?.toString()])
);
return {value, headers};
};
9 changes: 9 additions & 0 deletions tests/services/getOffset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Kafka} from 'kafkajs';

export const getOffset = async (kafka: Kafka, groupId: string, topic: string) => {
const admin = kafka.admin();
await admin.connect();
const metadata = await admin.fetchOffsets({groupId, topics: [topic]});
admin.disconnect();
return Number.parseInt(metadata[0]?.partitions[0]?.offset!);
};
8 changes: 8 additions & 0 deletions tests/services/produce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ProducerRecord} from 'kafkajs';
import {Orchestrator} from '../testcontainers/orchestrator.js';

export const produce = async (orchestrator: Orchestrator, record: ProducerRecord) => {
const producer = orchestrator.kafkaClient.producer();
await producer.connect();
await producer.send(record);
};
3 changes: 3 additions & 0 deletions tests/setupFilesAfterEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {jest} from '@jest/globals';

jest.retryTimes(3, {logErrorsBeforeRetry: true});
10 changes: 10 additions & 0 deletions tests/specs/__snapshots__/debounceByKey.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tests should debounce by key 1`] = `
{
"headers": {},
"value": {
"data": "bar",
},
}
`;
12 changes: 12 additions & 0 deletions tests/specs/__snapshots__/debounceBySpecificFields.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tests should debounce by specific fields 1`] = `
{
"headers": {},
"value": {
"data": {
"type": "bar",
},
},
}
`;
28 changes: 0 additions & 28 deletions tests/specs/__snapshots__/tests.spec.ts.snap

This file was deleted.

50 changes: 50 additions & 0 deletions tests/specs/debounceByKey.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import delay from 'delay';

import type {Orchestrator} from '../testcontainers/orchestrator.js';
import {start} from '../testcontainers/orchestrator.js';
import {produce} from '../services/produce.js';
import {getOffset} from '../services/getOffset.js';
import {consume} from '../services/consume.js';

const groupId = 'the-consumer';
const sourceTopic = 'test-input';
const targetTopic = 'test-output';

describe('tests', () => {
let orchestrator: Orchestrator;

beforeEach(
async () => {
orchestrator = await start(
{
KAFKA_BROKER: 'kafka:9092',
MONITORING_SERVER_PORT: '3000',
GROUP_ID: groupId,
COMMIT_INTERVAL_MS_CONFIG: '1000',
SOURCE_TOPIC: sourceTopic,
TARGET_TOPIC: targetTopic,
WINDOW_DURATION: '5',
},
[sourceTopic, targetTopic]
);
},
5 * 60 * 1000
);

afterEach(async () => {
if (!orchestrator) {
return;
}
await orchestrator.stop();
});

it('should debounce by key', async () => {
await produce(orchestrator, {topic: sourceTopic, messages: [{value: JSON.stringify({data: 'bar'}), key: 'barkey'}]});
await produce(orchestrator, {topic: sourceTopic, messages: [{value: JSON.stringify({data: 'bar'}), key: 'barkey'}]});

await delay(5000);

await expect(consume(orchestrator.kafkaClient, targetTopic)).resolves.toMatchSnapshot();
await expect(getOffset(orchestrator.kafkaClient, groupId, sourceTopic)).resolves.toBe(2);
});
});
52 changes: 52 additions & 0 deletions tests/specs/debounceBySpecificFields.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import delay from 'delay';

import type {Orchestrator} from '../testcontainers/orchestrator.js';
import {start} from '../testcontainers/orchestrator.js';
import {produce} from '../services/produce.js';
import {getOffset} from '../services/getOffset.js';
import {consume} from '../services/consume.js';

const groupId = 'test11';
const sourceTopic = 'test-input';
const targetTopic = 'test-output';

describe('tests', () => {
let orchestrator: Orchestrator;

beforeEach(
async () => {
orchestrator = await start(
{
KAFKA_BROKER: 'kafka:9092',
MONITORING_SERVER_PORT: '3000',
GROUP_ID: groupId,
COMMIT_INTERVAL_MS_CONFIG: '1000',
SOURCE_TOPIC: sourceTopic,
TARGET_TOPIC: targetTopic,
WINDOW_DURATION: '5',
OUTPUT_PARTITIONING_KEY: 'SPECIFIC_FIELDS',
OUTPUT_PARTITIONING_KEY_FIELDS: '/data/type',
},
[sourceTopic, targetTopic]
);
},
5 * 60 * 1000
);

afterEach(async () => {
if (!orchestrator) {
return;
}
await orchestrator.stop();
});

it('should debounce by specific fields', async () => {
await produce(orchestrator, {topic: sourceTopic, messages: [{value: JSON.stringify({data: {type: 'bar'}}), key: '1'}]});
await produce(orchestrator, {topic: sourceTopic, messages: [{value: JSON.stringify({data: {type: 'bar'}}), key: '2'}]});

await delay(5000);

await expect(consume(orchestrator.kafkaClient, targetTopic)).resolves.toMatchSnapshot();
await expect(getOffset(orchestrator.kafkaClient, groupId, sourceTopic)).resolves.toBe(2);
});
});
101 changes: 0 additions & 101 deletions tests/specs/tests.spec.ts

This file was deleted.

35 changes: 35 additions & 0 deletions tests/testcontainers/dafka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {StartedNetwork, StoppedTestContainer, Wait} from 'testcontainers';
import {GenericContainer} from 'testcontainers';
import fs from 'node:fs';

const startupTimeout = parseInt(process.env.STARTUP_TIMEOUT ?? '60000');

export interface ServiceContainer {
stop: () => Promise<StoppedTestContainer>;
}

export const dafka = async (
network: StartedNetwork,
env: Record<string, string>
): Promise<ServiceContainer> => {
const container = await new GenericContainer('bazel/src:image')
.withExposedPorts(3000)
.withNetwork(network)
.withEnvironment(env)
.withWaitStrategy(Wait.forLogMessage(`dafka-debounce-${env.GROUP_ID} started`))
.withStartupTimeout(startupTimeout)
.start();

if (process.env.DEBUG) {
try {
fs.truncateSync('service.log', 0);
} catch (err) {
fs.writeFileSync('service.log', "", { flag: "wx" });
}
await container.logs().then((logs) => logs.pipe(fs.createWriteStream('service.log')));
}

return {
stop: () => container.stop(),
};
};
Loading
Loading