1
+ import express from 'express' ;
2
+ import { Server as HttpServer } from 'http' ;
3
+
1
4
import { testServer } from '../../test/server.js' ;
2
5
import {
3
6
ApiKey ,
@@ -7,6 +10,23 @@ import {
7
10
} from './auth.js' ;
8
11
import Connection from './index.js' ;
9
12
13
+ import { createServer , Server as GrpcServer } from 'nice-grpc' ;
14
+ import {
15
+ HealthCheckRequest ,
16
+ HealthCheckResponse ,
17
+ HealthCheckResponse_ServingStatus ,
18
+ HealthDefinition ,
19
+ HealthServiceImplementation ,
20
+ } from '../proto/google/health/v1/health' ;
21
+ import { TenantsGetReply } from '../proto/v1/tenants' ;
22
+ import { WeaviateDefinition , WeaviateServiceImplementation } from '../proto/v1/weaviate' ;
23
+
24
+ import { WeaviateRequestTimeoutError } from '../errors.js' ;
25
+ import weaviate , { Collection , WeaviateClient } from '../index' ;
26
+ import { BatchObjectsReply } from '../proto/v1/batch.js' ;
27
+ import { BatchDeleteReply } from '../proto/v1/batch_delete.js' ;
28
+ import { SearchReply } from '../proto/v1/search_get.js' ;
29
+
10
30
describe ( 'mock server auth tests' , ( ) => {
11
31
const server = testServer ( ) ;
12
32
describe ( 'OIDC auth flows' , ( ) => {
@@ -197,3 +217,113 @@ describe('mock server auth tests', () => {
197
217
return server . close ( ) ;
198
218
} ) ;
199
219
} ) ;
220
+
221
+ const COLLECTION_NAME = 'TestCollectionTimeouts' ;
222
+
223
+ const makeRestApp = ( version : string ) => {
224
+ const httpApp = express ( ) ;
225
+ httpApp . get ( `/v1/schema/${ COLLECTION_NAME } ` , ( req , res ) =>
226
+ new Promise ( ( r ) => setTimeout ( r , 2000 ) ) . then ( ( ) => res . send ( { class : COLLECTION_NAME } ) )
227
+ ) ;
228
+ httpApp . get ( '/v1/meta' , ( req , res ) => res . send ( { version } ) ) ;
229
+ return httpApp ;
230
+ } ;
231
+
232
+ const makeGrpcApp = ( ) => {
233
+ const weaviateMockImpl : WeaviateServiceImplementation = {
234
+ tenantsGet : ( ) : Promise < TenantsGetReply > =>
235
+ new Promise ( ( r ) => {
236
+ setTimeout ( r , 2000 ) ;
237
+ } ) . then ( ( ) => {
238
+ return {
239
+ took : 5000 ,
240
+ tenants : [ ] ,
241
+ } ;
242
+ } ) ,
243
+ search : ( ) : Promise < SearchReply > =>
244
+ new Promise ( ( r ) => {
245
+ setTimeout ( r , 2000 ) ;
246
+ } ) . then ( ( ) => {
247
+ return {
248
+ results : [ ] ,
249
+ took : 5000 ,
250
+ groupByResults : [ ] ,
251
+ } ;
252
+ } ) ,
253
+ batchDelete : ( ) : Promise < BatchDeleteReply > =>
254
+ new Promise ( ( r ) => {
255
+ setTimeout ( r , 2000 ) ;
256
+ } ) . then ( ( ) => {
257
+ return {
258
+ took : 5000 ,
259
+ status : 'SUCCESS' ,
260
+ failed : 0 ,
261
+ matches : 0 ,
262
+ successful : 0 ,
263
+ objects : [ ] ,
264
+ } ;
265
+ } ) ,
266
+ batchObjects : ( ) : Promise < BatchObjectsReply > =>
267
+ new Promise ( ( r ) => {
268
+ setTimeout ( r , 2000 ) ;
269
+ } ) . then ( ( ) => {
270
+ return {
271
+ took : 5000 ,
272
+ errors : [ ] ,
273
+ } ;
274
+ } ) ,
275
+ } ;
276
+ const healthMockImpl : HealthServiceImplementation = {
277
+ check : ( request : HealthCheckRequest ) : Promise < HealthCheckResponse > =>
278
+ Promise . resolve ( HealthCheckResponse . create ( { status : HealthCheckResponse_ServingStatus . SERVING } ) ) ,
279
+ watch : jest . fn ( ) ,
280
+ } ;
281
+
282
+ const grpcApp = createServer ( ) ;
283
+ grpcApp . add ( WeaviateDefinition , weaviateMockImpl ) ;
284
+ grpcApp . add ( HealthDefinition , healthMockImpl ) ;
285
+
286
+ return grpcApp ;
287
+ } ;
288
+
289
+ const makeMockServers = async ( weaviateVersion : string , httpPort : number , grpcAddress : string ) => {
290
+ const rest = makeRestApp ( weaviateVersion ) ;
291
+ const grpc = makeGrpcApp ( ) ;
292
+ const server = await rest . listen ( httpPort ) ;
293
+ await grpc . listen ( grpcAddress ) ;
294
+ return { rest : server , grpc, express } ;
295
+ } ;
296
+
297
+ describe ( 'Mock testing of timeout behaviour' , ( ) => {
298
+ let servers : {
299
+ rest : HttpServer ;
300
+ grpc : GrpcServer ;
301
+ } ;
302
+ let client : WeaviateClient ;
303
+ let collection : Collection ;
304
+
305
+ beforeAll ( async ( ) => {
306
+ servers = await makeMockServers ( '1.28.2' , 8954 , 'localhost:8955' ) ;
307
+ client = await weaviate . connectToLocal ( { port : 8954 , grpcPort : 8955 , timeout : { query : 1 , insert : 1 } } ) ;
308
+ collection = client . collections . get ( COLLECTION_NAME ) ;
309
+ } ) ;
310
+
311
+ it ( 'should timeout when calling REST GET v1/schema' , ( ) =>
312
+ expect ( collection . config . get ( ) ) . rejects . toThrow ( WeaviateRequestTimeoutError ) ) ;
313
+
314
+ it ( 'should timeout when calling gRPC TenantsGet' , ( ) =>
315
+ expect ( collection . tenants . get ( ) ) . rejects . toThrow ( WeaviateRequestTimeoutError ) ) ;
316
+
317
+ it ( 'should timeout when calling gRPC Search' , ( ) =>
318
+ expect ( collection . query . fetchObjects ( ) ) . rejects . toThrow ( WeaviateRequestTimeoutError ) ) ;
319
+
320
+ it ( 'should timeout when calling gRPC BatchObjects' , ( ) =>
321
+ expect ( collection . data . insertMany ( [ { thing : 'what' } ] ) ) . rejects . toThrow ( WeaviateRequestTimeoutError ) ) ;
322
+
323
+ it ( 'should timeout when calling gRPC BatchDelete' , ( ) =>
324
+ expect ( collection . data . deleteMany ( collection . filter . byId ( ) . equal ( '123' as any ) ) ) . rejects . toThrow (
325
+ WeaviateRequestTimeoutError
326
+ ) ) ;
327
+
328
+ afterAll ( ( ) => Promise . all ( [ servers . rest . close ( ) , servers . grpc . shutdown ( ) ] ) ) ;
329
+ } ) ;
0 commit comments