|
| 1 | +import {Channel} from '@grpc/grpc-js'; |
| 2 | +import {ConnectivityState} from '@grpc/grpc-js/build/src/connectivity-state'; |
| 3 | +import {GrpcClientWithChannel} from '../../../src/internal/grpc/grpc-client-wrapper'; |
| 4 | +import {IdleGrpcClientWrapper} from '../../../src/internal/grpc/idle-grpc-client-wrapper'; |
| 5 | +import { |
| 6 | + DefaultMomentoLoggerFactory, |
| 7 | + DefaultMomentoLoggerLevel, |
| 8 | +} from '../../../src'; |
| 9 | + |
| 10 | +const createMockChannel = (state: ConnectivityState): jest.Mocked<Channel> => { |
| 11 | + return { |
| 12 | + getConnectivityState: jest.fn(() => state), |
| 13 | + } as unknown as jest.Mocked<Channel>; |
| 14 | +}; |
| 15 | + |
| 16 | +const createMockGrpcClient = ( |
| 17 | + channel: jest.Mocked<Channel> |
| 18 | +): jest.Mocked<GrpcClientWithChannel> => ({ |
| 19 | + close: jest.fn(), |
| 20 | + getChannel: jest.fn(() => channel), |
| 21 | +}); |
| 22 | + |
| 23 | +describe('IdleGrpcClientWrapper', () => { |
| 24 | + let now: number; |
| 25 | + const loggerFactory = new DefaultMomentoLoggerFactory( |
| 26 | + DefaultMomentoLoggerLevel.INFO |
| 27 | + ); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.useFakeTimers(); |
| 31 | + now = Date.now(); |
| 32 | + jest.setSystemTime(now); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + jest.useRealTimers(); |
| 37 | + jest.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns the same client if not idle and channel is healthy', () => { |
| 41 | + const channel = createMockChannel(ConnectivityState.READY); |
| 42 | + const client = createMockGrpcClient(channel); |
| 43 | + |
| 44 | + const wrapper = new IdleGrpcClientWrapper<GrpcClientWithChannel>({ |
| 45 | + clientFactoryFn: () => client, |
| 46 | + loggerFactory: loggerFactory, |
| 47 | + maxIdleMillis: 10000, |
| 48 | + }); |
| 49 | + |
| 50 | + wrapper.getClient(); // first call to getClient() initializes the client |
| 51 | + jest.advanceTimersByTime(500); // simulate 500ms |
| 52 | + wrapper.getClient(); // second call should return the same client |
| 53 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 54 | + expect(client.close).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('recreates the client if the gRPC channel is in TRANSIENT_FAILURE', () => { |
| 58 | + // create a mock client whose channel is in a bad state |
| 59 | + const badChannel = createMockChannel(ConnectivityState.TRANSIENT_FAILURE); |
| 60 | + const badClient = createMockGrpcClient(badChannel); |
| 61 | + |
| 62 | + const factory = jest |
| 63 | + .fn<GrpcClientWithChannel, []>() |
| 64 | + .mockReturnValue(badClient); |
| 65 | + |
| 66 | + const wrapper = new IdleGrpcClientWrapper<GrpcClientWithChannel>({ |
| 67 | + clientFactoryFn: factory, |
| 68 | + loggerFactory, |
| 69 | + maxIdleMillis: 10000, |
| 70 | + }); |
| 71 | + |
| 72 | + // call getClient(), which should recreate the client due to bad channel |
| 73 | + wrapper.getClient(); |
| 74 | + // factory should be called twice (initial + reconnection) |
| 75 | + expect(factory).toHaveBeenCalledTimes(2); |
| 76 | + // the first client should be closed |
| 77 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 78 | + expect(badClient.close).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('recreates the client if it exceeds maxIdleMillis', () => { |
| 82 | + const initialChannel = createMockChannel(ConnectivityState.READY); |
| 83 | + const newChannel = createMockChannel(ConnectivityState.READY); |
| 84 | + |
| 85 | + const initialClient = createMockGrpcClient(initialChannel); |
| 86 | + const newClient = createMockGrpcClient(newChannel); |
| 87 | + |
| 88 | + const factory = jest |
| 89 | + .fn<GrpcClientWithChannel, []>() |
| 90 | + .mockReturnValueOnce(initialClient) |
| 91 | + .mockReturnValueOnce(newClient); |
| 92 | + |
| 93 | + const wrapper = new IdleGrpcClientWrapper<GrpcClientWithChannel>({ |
| 94 | + clientFactoryFn: factory, |
| 95 | + loggerFactory, |
| 96 | + maxIdleMillis: 10_000, |
| 97 | + maxClientAgeMillis: 60_000, // longer, so it won’t interfere |
| 98 | + }); |
| 99 | + |
| 100 | + // Initial client use |
| 101 | + const c1 = wrapper.getClient(); |
| 102 | + expect(c1).toBe(initialClient); |
| 103 | + |
| 104 | + // Advance past idle threshold only |
| 105 | + jest.advanceTimersByTime(15_000); |
| 106 | + |
| 107 | + const c2 = wrapper.getClient(); |
| 108 | + expect(c2).toBe(newClient); |
| 109 | + expect(factory).toHaveBeenCalledTimes(2); |
| 110 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 111 | + expect(initialClient.close).toHaveBeenCalledTimes(1); |
| 112 | + }); |
| 113 | + |
| 114 | + it('recreates the client if it exceeds maxClientAgeMillis', () => { |
| 115 | + const initialChannel = createMockChannel(ConnectivityState.READY); |
| 116 | + const newChannel = createMockChannel(ConnectivityState.READY); |
| 117 | + |
| 118 | + const initialClient = createMockGrpcClient(initialChannel); |
| 119 | + const newClient = createMockGrpcClient(newChannel); |
| 120 | + |
| 121 | + const factory = jest |
| 122 | + .fn<GrpcClientWithChannel, []>() |
| 123 | + .mockReturnValueOnce(initialClient) |
| 124 | + .mockReturnValueOnce(newClient); |
| 125 | + |
| 126 | + const wrapper = new IdleGrpcClientWrapper<GrpcClientWithChannel>({ |
| 127 | + clientFactoryFn: factory, |
| 128 | + loggerFactory, |
| 129 | + maxIdleMillis: 60_000, // longer, so it won’t interfere |
| 130 | + maxClientAgeMillis: 10_000, |
| 131 | + }); |
| 132 | + |
| 133 | + // Initial use |
| 134 | + const c1 = wrapper.getClient(); |
| 135 | + expect(c1).toBe(initialClient); |
| 136 | + |
| 137 | + // Regular usage, but pass age threshold |
| 138 | + jest.advanceTimersByTime(12_000); |
| 139 | + |
| 140 | + const c2 = wrapper.getClient(); |
| 141 | + expect(c2).toBe(newClient); |
| 142 | + expect(factory).toHaveBeenCalledTimes(2); |
| 143 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 144 | + expect(initialClient.close).toHaveBeenCalledTimes(1); |
| 145 | + }); |
| 146 | +}); |
0 commit comments