Skip to content

Commit

Permalink
Add tests for the typescript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
tulios committed Jun 13, 2019
1 parent a7c5595 commit 5c33472
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ azure-pipelines.yml
logo
*.tgz
.vscode/
*.java
*.java
types/tests.ts
types/tsconfig.json
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"printWidth": 100,
"parser": "babylon",
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"test:group:admin:ci": "JEST_JUNIT_OUTPUT=test-report.xml ./scripts/testWithKafka.sh \"yarn test:group:admin --ci --maxWorkers=4 --no-watchman\"",
"test:group:producer:ci": "JEST_JUNIT_OUTPUT=test-report.xml ./scripts/testWithKafka.sh \"yarn test:group:producer --ci --maxWorkers=4 --no-watchman\"",
"test:group:consumer:ci": "JEST_JUNIT_OUTPUT=test-report.xml ./scripts/testWithKafka.sh \"yarn test:group:consumer --ci --maxWorkers=4 --no-watchman\"",
"test:group:others:ci": "JEST_JUNIT_OUTPUT=test-report.xml ./scripts/testWithKafka.sh \"yarn test:group:others --ci --maxWorkers=4 --no-watchman\""
"test:group:others:ci": "JEST_JUNIT_OUTPUT=test-report.xml ./scripts/testWithKafka.sh \"yarn test:group:others --ci --maxWorkers=4 --no-watchman\"",
"test:types": "./node_modules/.bin/tsc -p types/ && rm types/*.js"
},
"devDependencies": {
"@types/node": "^12.0.8",
Expand Down
127 changes: 127 additions & 0 deletions types/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as fs from 'fs'

import { Kafka, PartitionAssigners, logLevel, CompressionTypes, CompressionCodecs } from './index'

const { roundRobin } = PartitionAssigners

// COMMON
const host = 'localhost'
const topic = 'topic-test'

const kafka = new Kafka({
logLevel: logLevel.INFO,
brokers: [`${host}:9094`, `${host}:9097`, `${host}:9100`],
clientId: 'example-consumer',
ssl: {
ca: [fs.readFileSync('./testHelpers/certs/cert-signed', 'utf-8')],
},
sasl: {
mechanism: 'plain',
username: 'test',
password: 'testtest',
},
})

// CONSUMER
const consumer = kafka.consumer({ groupId: 'test-group' })

const runConsumer = async () => {
await consumer.connect()
await consumer.subscribe({ topic })
await consumer.run({
eachBatch: async ({
batch,
resolveOffset,
heartbeat,
commitOffsetsIfNecessary,
uncommittedOffsets,
isRunning,
isStale,
}) => {
resolveOffset('123')
await heartbeat()
commitOffsetsIfNecessary({
topics: [
{
topic: 'topic-name',
partitions: [{ partition: 0, offset: '500' }],
},
],
})
uncommittedOffsets()
isRunning()
isStale()
console.log(batch)
},
eachMessage: async ({ topic, partition, message }) => {
const prefix = `${topic}[${partition} | ${message.offset}] / ${message.timestamp}`
console.log(`- ${prefix} ${message.key}#${message.value}`)
},
})
await consumer.disconnect()
}

runConsumer().catch(e => console.error(`[example/consumer] ${e.message}`, e))

// PRODUCER
const producer = kafka.producer({ allowAutoTopicCreation: true })

const getRandomNumber = () => Math.round(Math.random() * 1000)
const createMessage = (num: number) => ({
key: `key-${num}`,
value: `value-${num}-${new Date().toISOString()}`,
})

const sendMessage = () => {
return producer
.send({
topic,
compression: CompressionTypes.GZIP,
messages: Array(getRandomNumber())
.fill(0)
.map(_ => createMessage(getRandomNumber())),
})
.then(console.log)
.catch(e => console.error(`[example/producer] ${e.message}`, e))
}

const runProducer = async () => {
await producer.connect()
setInterval(sendMessage, 3000)
await producer.disconnect()
}

runProducer().catch(e => console.error(`[example/producer] ${e.message}`, e))

// ADMIN
const admin = kafka.admin({ retry: { retries: 10 } })

const runAdmin = async () => {
await admin.connect()
await admin.fetchTopicMetadata({ topics: ['string'] })
await admin.createTopics({
topics: [{ topic, numPartitions: 10, replicationFactor: 1 }],
timeout: 30000,
waitForLeaders: true,
})
await admin.disconnect()
}

runAdmin().catch(e => console.error(`[example/admin] ${e.message}`, e))

// OTHERS
;async () => {
await producer.send({
topic: 'topic-name',
compression: CompressionTypes.GZIP,
messages: [{ key: 'key1', value: 'hello world!' }],
})
}

const SnappyCodec: any = undefined
CompressionCodecs[CompressionTypes.Snappy] = SnappyCodec

kafka.consumer({
groupId: 'my-group',
partitionAssigners: [roundRobin],
})
19 changes: 19 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true
},
"files": [
"index.d.ts",
"tests.ts"
]
}

0 comments on commit 5c33472

Please sign in to comment.