Skip to content

Commit 98c71c3

Browse files
committed
test
1 parent 909a285 commit 98c71c3

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,112 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
824824
expect(roleIndexes.find(idx => idx.name === 'name_1')).toBeDefined();
825825
});
826826
});
827+
828+
describe('clientLogEvents', () => {
829+
it('should log MongoDB client events when configured', async () => {
830+
const logger = require('../lib/logger').logger;
831+
const logSpy = spyOn(logger, 'warn');
832+
833+
const clientLogEvents = [
834+
{
835+
name: 'serverDescriptionChanged',
836+
keys: ['address'],
837+
logLevel: 'warn',
838+
},
839+
];
840+
841+
const adapter = new MongoStorageAdapter({
842+
uri: databaseURI,
843+
mongoOptions: { clientLogEvents },
844+
});
845+
846+
// Connect to trigger event listeners setup
847+
await adapter.connect();
848+
849+
// Manually trigger the event to test the listener
850+
const mockEvent = {
851+
address: 'localhost:27017',
852+
previousDescription: { type: 'Unknown' },
853+
newDescription: { type: 'Standalone' },
854+
};
855+
856+
adapter.client.emit('serverDescriptionChanged', mockEvent);
857+
858+
// Verify the log was called with the correct message
859+
expect(logSpy).toHaveBeenCalledWith(
860+
jasmine.stringMatching(/MongoDB client event serverDescriptionChanged:.*"address":"localhost:27017"/)
861+
);
862+
863+
await adapter.handleShutdown();
864+
});
865+
866+
it('should log entire event when keys are not specified', async () => {
867+
const logger = require('../lib/logger').logger;
868+
const logSpy = spyOn(logger, 'info');
869+
870+
const clientLogEvents = [
871+
{
872+
name: 'connectionPoolReady',
873+
logLevel: 'info',
874+
},
875+
];
876+
877+
const adapter = new MongoStorageAdapter({
878+
uri: databaseURI,
879+
mongoOptions: { clientLogEvents },
880+
});
881+
882+
await adapter.connect();
883+
884+
const mockEvent = {
885+
address: 'localhost:27017',
886+
options: { maxPoolSize: 100 },
887+
};
888+
889+
adapter.client.emit('connectionPoolReady', mockEvent);
890+
891+
expect(logSpy).toHaveBeenCalledWith(
892+
jasmine.stringMatching(/MongoDB client event connectionPoolReady:.*"address":"localhost:27017".*"options"/)
893+
);
894+
895+
await adapter.handleShutdown();
896+
});
897+
898+
it('should extract nested keys using dot notation', async () => {
899+
const logger = require('../lib/logger').logger;
900+
const logSpy = spyOn(logger, 'warn');
901+
902+
const clientLogEvents = [
903+
{
904+
name: 'topologyDescriptionChanged',
905+
keys: ['previousDescription.type', 'newDescription.type', 'newDescription.servers.size'],
906+
logLevel: 'warn',
907+
},
908+
];
909+
910+
const adapter = new MongoStorageAdapter({
911+
uri: databaseURI,
912+
mongoOptions: { clientLogEvents },
913+
});
914+
915+
await adapter.connect();
916+
917+
const mockEvent = {
918+
topologyId: 1,
919+
previousDescription: { type: 'Unknown' },
920+
newDescription: {
921+
type: 'ReplicaSetWithPrimary',
922+
servers: { size: 3 },
923+
},
924+
};
925+
926+
adapter.client.emit('topologyDescriptionChanged', mockEvent);
927+
928+
expect(logSpy).toHaveBeenCalledWith(
929+
jasmine.stringMatching(/MongoDB client event topologyDescriptionChanged:.*"previousDescription.type":"Unknown".*"newDescription.type":"ReplicaSetWithPrimary".*"newDescription.servers.size":3/)
930+
);
931+
932+
await adapter.handleShutdown();
933+
});
934+
});
827935
});

0 commit comments

Comments
 (0)