@@ -23,6 +23,7 @@ import { mochaTestServer } from '@mongodb-js/compass-test-server';
23
23
import type { SearchIndex } from './search-index-detail-helper' ;
24
24
import { range } from 'lodash' ;
25
25
import ConnectionString from 'mongodb-connection-string-url' ;
26
+ import type { DataServiceImplLogger , MongoLogId } from './logger' ;
26
27
27
28
const { expect } = chai ;
28
29
chai . use ( chaiAsPromised ) ;
@@ -93,6 +94,118 @@ describe('DataService', function () {
93
94
. drop ( ) ;
94
95
} ) ;
95
96
97
+ describe ( '#connect' , function ( ) {
98
+ const connectionStartedId = 1_001_000_014 ;
99
+ const connectionSucceededId = 1_001_000_015 ;
100
+ const connectionFailedId = 1_001_000_359 ;
101
+ const logs : {
102
+ info : [
103
+ component : string ,
104
+ id : MongoLogId ,
105
+ context : string ,
106
+ message : string ,
107
+ attr ?: unknown
108
+ ] ;
109
+ } [ ] = [ ] ;
110
+ const logCollector : DataServiceImplLogger = {
111
+ error : ( ) => { } ,
112
+ info : ( ...args ) => logs . push ( { info : args } ) ,
113
+ debug : ( ) => { } ,
114
+ warn : ( ) => { } ,
115
+ fatal : ( ) => { } ,
116
+ } ;
117
+
118
+ let dataServiceLogTest ;
119
+
120
+ beforeEach ( function ( ) {
121
+ logs . length = 0 ;
122
+ } ) ;
123
+
124
+ afterEach ( async function ( ) {
125
+ await dataServiceLogTest ?. disconnect ( ) ;
126
+ dataServiceLogTest = undefined ;
127
+ } ) ;
128
+
129
+ // The log and connection ids in this test are used by CompassWeb to measure connect time metrics.
130
+ // The intention of these tests is not to restrict any changes to the logs or ids.
131
+ // If a change to the ids is necessary please inform the appropriate team and they can adjust before pulling in the change.
132
+ it ( 'when connecting succeeds there is a start and success log with a connectionId' , async function ( ) {
133
+ dataServiceLogTest = new DataServiceImpl (
134
+ connectionOptions ,
135
+ logCollector
136
+ ) ;
137
+ await dataServiceLogTest . connect ( ) ;
138
+
139
+ expect ( logs . map ( ( { info : [ , id ] } ) => id . __value ) ) . to . include (
140
+ connectionStartedId
141
+ ) ;
142
+ expect ( logs . map ( ( { info : [ , id ] } ) => id . __value ) ) . to . include (
143
+ connectionSucceededId
144
+ ) ;
145
+
146
+ const startedLog = logs . find (
147
+ ( { info : [ , id ] } ) => id . __value === connectionStartedId
148
+ ) ;
149
+ const succeededLog = logs . find (
150
+ ( { info : [ , id ] } ) => id . __value === connectionSucceededId
151
+ ) ;
152
+
153
+ const { info : [ , , , , startedAttr ] = [ ] } = startedLog ?? { } ;
154
+ expect ( startedAttr ) . to . have . property (
155
+ 'connectionId' ,
156
+ dataServiceLogTest . _id
157
+ ) ;
158
+ const { info : [ , , , , succeededAttr ] = [ ] } = succeededLog ?? { } ;
159
+ expect ( succeededAttr ) . to . have . property (
160
+ 'connectionId' ,
161
+ dataServiceLogTest . _id
162
+ ) ;
163
+ } ) ;
164
+
165
+ it ( 'when connecting fails there is a start and failure log with a connectionId' , async function ( ) {
166
+ dataServiceLogTest = new DataServiceImpl (
167
+ {
168
+ connectionString :
169
+ 'mongodb://iLoveJavascript?serverSelectionTimeoutMS=5' ,
170
+ lookup : ( ) => {
171
+ throw new Error ( 'test error' ) ;
172
+ } ,
173
+ } ,
174
+ logCollector
175
+ ) ;
176
+
177
+ const result = await dataServiceLogTest
178
+ . connect ( )
179
+ . catch ( ( error ) => error ) ;
180
+ expect ( result ) . to . be . instanceOf ( Error ) ;
181
+
182
+ expect ( logs . map ( ( { info : [ , id ] } ) => id . __value ) ) . to . include (
183
+ connectionStartedId
184
+ ) ;
185
+ expect ( logs . map ( ( { info : [ , id ] } ) => id . __value ) ) . to . include (
186
+ connectionFailedId
187
+ ) ;
188
+
189
+ const startedLog = logs . find (
190
+ ( { info : [ , id ] } ) => id . __value === connectionStartedId
191
+ ) ;
192
+ const failedLog = logs . find (
193
+ ( { info : [ , id ] } ) => id . __value === connectionFailedId
194
+ ) ;
195
+
196
+ const { info : [ , , , , startedAttr ] = [ ] } = startedLog ?? { } ;
197
+ expect ( startedAttr ) . to . have . property (
198
+ 'connectionId' ,
199
+ dataServiceLogTest . _id
200
+ ) ;
201
+ const { info : [ , , , , succeededAttr ] = [ ] } = failedLog ?? { } ;
202
+ expect ( succeededAttr ) . to . have . property (
203
+ 'connectionId' ,
204
+ dataServiceLogTest . _id
205
+ ) ;
206
+ } ) ;
207
+ } ) ;
208
+
96
209
describe ( '#isConnected' , function ( ) {
97
210
let dataServiceIsConnected : DataService ;
98
211
0 commit comments