test('create new partition', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await admin.connect() await expect( admin.createTopics({ waitForLeaders: false, topics: [{ topic: topicName }], }) ).resolves.toEqual(true) await expect( admin.createPartitions({ topicPartitions: [{ topic: topicName, count: 2 }], }) ).resolves.not.toThrow() })
test('retries if the controller has moved', async () => { const cluster = createCluster() const broker = { createPartitions: jest.fn(() => true) } cluster.refreshMetadata = jest.fn() cluster.findControllerBroker = jest .fn() .mockImplementationOnce(() => { throw new KafkaJSProtocolError(createErrorFromCode(NOT_CONTROLLER)) }) .mockImplementationOnce(() => broker) admin = createAdmin({ cluster, logger: newLogger() }) await expect( admin.createPartitions({ topicPartitions: [{ topic: topicName, count: 2 }], }) ).resolves.not.toThrow() expect(cluster.refreshMetadata).toHaveBeenCalledTimes(2) expect(cluster.findControllerBroker).toHaveBeenCalledTimes(2) expect(broker.createPartitions).toHaveBeenCalledTimes(1) })
it('should throw when ?forward is invalid', () => { expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080' })).toThrow(); expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080?forward' })).toThrow(); expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080?forward=???' })).toThrow(); expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080?forward=127.0.0.1' })).toThrow(); expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080?forward=127.0.0.1:-1' })).toThrow(); expect(() => Config.test({ ...baseConf, service: 'tcp://localhost:1080?forward=127.0.0.1:1083' })).not.toThrow(); });
expect(() => Config.testOnServer({ ...baseConf, key: null })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, key: '' })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, key: 'a' })).not.toThrow(); }); expect(() => Config.testOnServer({ ...baseConf, presets: null })).toThrow(); }); expect(() => Config.testOnServer({ ...baseConf, presets: [] })).toThrow(); }); expect(() => Config.testOnServer({ ...baseConf, presets: [{}] })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, presets: [{ name: '' }] })).toThrow(); }); expect(() => Config.testOnServer({ ...baseConf, presets: [{ name: 'ss-base', params: '' }] })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, presets: [{ name: 'ss-base', params: [] }] })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, presets: [{ name: 'ss-base', params: null }] })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, presets: [{ name: 'ss-base', params: {} }] })).not.toThrow(); }); expect(() => Config.testOnServer({ ...baseConf, redirect: null })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, redirect: '123' })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, redirect: '*:80' })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, redirect: '123:-1' })).toThrow(); expect(() => Config.testOnServer({ ...baseConf, redirect: 'bing.com:80' })).not.toThrow(); expect(() => Config.testOnServer({ ...baseConf, redirect: '' })).not.toThrow(); });
describe('Config#testOnClient', () => { it('should throw when server is invalid', () => { expect(() => Config.testOnClient({ service: 'http://localhost:1080', server: '' })).toThrow(); expect(() => Config.testOnClient({ service: 'http://localhost:1080', server: [] })).toThrow(); }); const baseConf = { server: { service: 'tcp://127.0.0.1:1082', key: 'abc', presets: [{ name: 'ss-base', }], }, }; it('should throw when service is not provided', () => { expect(() => Config.testOnClient({ ...baseConf })).toThrow(); }); it('should throw when service is invalid', () => { expect(() => Config.testOnClient({ ...baseConf, service: 'xxx' })).toThrow(); expect(() => Config.testOnClient({ ...baseConf, service: 'ws://' })).toThrow(); expect(() => Config.testOnClient({ ...baseConf, service: 'tcp://?' })).toThrow(); expect(() => Config.testOnClient({ ...baseConf, service: 'tcp://abc' })).toThrow(); expect(() => Config.testOnClient({ ...baseConf, service: 'http://abc:1' })).not.toThrow(); }); });
it('should throw when timeout(if provided) is invalid', () => { expect(() => Config.test({ ...baseConf, timeout: '0' })).toThrow(); expect(() => Config.test({ ...baseConf, timeout: 0 })).toThrow(); expect(() => Config.test({ ...baseConf, timeout: 1 })).not.toThrow(); expect(() => Config.test({ ...baseConf, timeout: 61 })).not.toThrow(); });
it('should throw when dns(is provided) is invalid', () => { expect(() => Config.test({ ...baseConf, dns: null })).toThrow(); expect(() => Config.test({ ...baseConf, dns: [''] })).toThrow(); expect(() => Config.test({ ...baseConf, dns: ['localhost'] })).toThrow(); expect(() => Config.test({ ...baseConf, dns: [] })).not.toThrow(); expect(() => Config.test({ ...baseConf, dns: ['8.8.8.8'] })).not.toThrow(); });
it('should throw when dns_expire(is provided) is invalid', () => { expect(() => Config.test({ ...baseConf, dns_expire: '0' })).toThrow(); expect(() => Config.test({ ...baseConf, dns_expire: -1 })).toThrow(); expect(() => Config.test({ ...baseConf, dns_expire: 24 * 60 * 60 })).not.toThrow(); expect(() => Config.test({ ...baseConf, dns_expire: 24 * 60 * 60 + 1 })).not.toThrow(); });