Browse Source

Merge pull request #60 from nameczz/nameczz/dev

fix test and check connection method
nameczz 3 years ago
parent
commit
25bd12ba50

+ 8 - 0
client/src/components/layout/GlobalEffect.tsx

@@ -2,12 +2,15 @@ import React, { useContext } from 'react';
 import axiosInstance from '../../http/Axios';
 import axiosInstance from '../../http/Axios';
 import { rootContext } from '../../context/Root';
 import { rootContext } from '../../context/Root';
 import { CODE_STATUS } from '../../consts/Http';
 import { CODE_STATUS } from '../../consts/Http';
+import { authContext } from '../../context/Auth';
+import { MILVUS_ADDRESS } from '../../consts/Localstorage';
 
 
 let axiosResInterceptor: number | null = null;
 let axiosResInterceptor: number | null = null;
 // let timer: Record<string, ReturnType<typeof setTimeout> | number>[] = [];
 // let timer: Record<string, ReturnType<typeof setTimeout> | number>[] = [];
 // we only take side effect here, nothing else
 // we only take side effect here, nothing else
 const GlobalEffect = (props: { children: React.ReactNode }) => {
 const GlobalEffect = (props: { children: React.ReactNode }) => {
   const { openSnackBar } = useContext(rootContext);
   const { openSnackBar } = useContext(rootContext);
+  const { setIsAuth, setAddress } = useContext(authContext);
 
 
   // catch axios error here
   // catch axios error here
   if (axiosResInterceptor === null) {
   if (axiosResInterceptor === null) {
@@ -25,6 +28,11 @@ const GlobalEffect = (props: { children: React.ReactNode }) => {
         switch (response.status) {
         switch (response.status) {
           case CODE_STATUS.UNAUTHORIZED:
           case CODE_STATUS.UNAUTHORIZED:
             return Promise.reject(error);
             return Promise.reject(error);
+          case CODE_STATUS.FORBIDDEN:
+            setIsAuth(false);
+            setAddress('');
+            window.localStorage.removeItem(MILVUS_ADDRESS);
+            break;
           default:
           default:
             break;
             break;
         }
         }

+ 1 - 0
client/src/consts/Http.ts

@@ -1,6 +1,7 @@
 export enum CODE_STATUS {
 export enum CODE_STATUS {
   SUCCESS = 200,
   SUCCESS = 200,
   UNAUTHORIZED = 401,
   UNAUTHORIZED = 401,
+  FORBIDDEN = 403,
 }
 }
 
 
 export const START_LOADING_TIME = 350;
 export const START_LOADING_TIME = 350;

+ 2 - 1
client/src/context/Auth.tsx

@@ -16,7 +16,7 @@ export const AuthProvider = (props: { children: React.ReactNode }) => {
   const [address, setAddress] = useState<string>(
   const [address, setAddress] = useState<string>(
     window.localStorage.getItem(MILVUS_ADDRESS) || ''
     window.localStorage.getItem(MILVUS_ADDRESS) || ''
   );
   );
-  const [isAuth, setIsAuth] = useState<boolean>(false);
+  const [isAuth, setIsAuth] = useState<boolean>(address !== '');
   // const isAuth = useMemo(() => !!address, [address]);
   // const isAuth = useMemo(() => !!address, [address]);
 
 
   useEffect(() => {
   useEffect(() => {
@@ -29,6 +29,7 @@ export const AuthProvider = (props: { children: React.ReactNode }) => {
       try {
       try {
         const res = await MilvusHttp.check(milvusAddress);
         const res = await MilvusHttp.check(milvusAddress);
         setAddress(res.connected ? milvusAddress : '');
         setAddress(res.connected ? milvusAddress : '');
+        res.connected && setIsAuth(true);
         if (!res.connected) {
         if (!res.connected) {
           window.localStorage.removeItem(MILVUS_ADDRESS);
           window.localStorage.removeItem(MILVUS_ADDRESS);
         }
         }

+ 0 - 2
client/src/http/Collection.ts

@@ -32,8 +32,6 @@ export class CollectionHttp extends BaseModel implements CollectionView {
   static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
   static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
   static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
   static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
 
 
-  static CHECK_URL = '/milvus/check';
-
   constructor(props: CollectionView) {
   constructor(props: CollectionView) {
     super(props);
     super(props);
     Object.assign(this, props);
     Object.assign(this, props);

+ 2 - 5
server/generate-csv.ts

@@ -3,10 +3,7 @@ import { createObjectCsvWriter as createCsvWriter } from 'csv-writer';
 // use to test vector insert
 // use to test vector insert
 const csvWriter = createCsvWriter({
 const csvWriter = createCsvWriter({
   path: './vectors.csv',
   path: './vectors.csv',
-  header: [
-    { id: 'vector', title: 'vector' },
-    { id: 'age', title: 'age' },
-  ],
+  header: [{ id: 'vector', title: 'vector' }],
 });
 });
 
 
 const records = [];
 const records = [];
@@ -23,7 +20,7 @@ const generateVector = (dimension: number) => {
 
 
 while (records.length < 50000) {
 while (records.length < 50000) {
   const value = generateVector(4);
   const value = generateVector(4);
-  records.push({ vector: value, age: records.length });
+  records.push({ vector: value });
 }
 }
 
 
 csvWriter
 csvWriter

+ 4 - 1
server/src/__tests__/collections/collections.service.test.ts

@@ -34,7 +34,10 @@ describe('Test collections service', () => {
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
 
 
-    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
+    await milvusService.connectMilvus(
+      { address: mockAddress },
+      insightCacheForTest
+    );
     service = new CollectionsService(milvusService);
     service = new CollectionsService(milvusService);
   });
   });
 
 

+ 4 - 4
server/src/__tests__/crons/crons.service.test.ts

@@ -45,7 +45,10 @@ describe('test crons service', () => {
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
 
 
-    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
+    await milvusService.connectMilvus(
+      { address: mockAddress },
+      insightCacheForTest
+    );
 
 
     collectionService = new CollectionsService(milvusService);
     collectionService = new CollectionsService(milvusService);
 
 
@@ -149,9 +152,6 @@ describe('test crons service', () => {
     // reset setup to trigger error
     // reset setup to trigger error
     const newCollectionService = new CollectionsService(milvusService);
     const newCollectionService = new CollectionsService(milvusService);
     const newSchedulerRegistry = new SchedulerRegistry([]);
     const newSchedulerRegistry = new SchedulerRegistry([]);
-    newCollectionService.getAllCollections = () => {
-      throw new Error('error');
-    };
 
 
     const newCronsService = new CronsService(
     const newCronsService = new CronsService(
       newCollectionService,
       newCollectionService,

+ 8 - 15
server/src/__tests__/milvus/milvus.service.test.ts

@@ -25,20 +25,13 @@ describe('Test Milvus service', () => {
   });
   });
 
 
   test('test connectMilvus method', async () => {
   test('test connectMilvus method', async () => {
-    const res = await service.connectMilvus(mockAddress, insightCacheForTest);
+    const res = await service.connectMilvus(
+      { address: mockAddress },
+      insightCacheForTest
+    );
     expect(res.address).toBe(mockAddress);
     expect(res.address).toBe(mockAddress);
   });
   });
 
 
-  test('test connectMilvus method error', async () => {
-    try {
-      await service.connectMilvus('', insightCacheForTest);
-    } catch (err) {
-      expect(err.message).toBe(
-        'Connect milvus failed, check your milvus address.'
-      );
-    }
-  });
-
   test('test checkMilvus when not connect to Milvus', () => {
   test('test checkMilvus when not connect to Milvus', () => {
     try {
     try {
       service.checkMilvus();
       service.checkMilvus();
@@ -49,7 +42,7 @@ describe('Test Milvus service', () => {
 
 
   test('test checkConnect method', async () => {
   test('test checkConnect method', async () => {
     // mock connect first
     // mock connect first
-    await service.connectMilvus(mockAddress, insightCacheForTest);
+    await service.connectMilvus({ address: mockAddress }, insightCacheForTest);
     // different address
     // different address
     const errorRes = await service.checkConnect('123', insightCacheForTest);
     const errorRes = await service.checkConnect('123', insightCacheForTest);
     expect(errorRes.connected).toBeFalsy();
     expect(errorRes.connected).toBeFalsy();
@@ -58,7 +51,7 @@ describe('Test Milvus service', () => {
   });
   });
 
 
   test('test managers after connected', async () => {
   test('test managers after connected', async () => {
-    await service.connectMilvus(mockAddress, insightCacheForTest);
+    await service.connectMilvus({ address: mockAddress }, insightCacheForTest);
     expect(service.collectionManager).toBeDefined();
     expect(service.collectionManager).toBeDefined();
     expect(service.partitionManager).toBeDefined();
     expect(service.partitionManager).toBeDefined();
     expect(service.indexManager).toBeDefined();
     expect(service.indexManager).toBeDefined();
@@ -66,14 +59,14 @@ describe('Test Milvus service', () => {
   });
   });
 
 
   test('test flush method', async () => {
   test('test flush method', async () => {
-    await service.connectMilvus(mockAddress, insightCacheForTest);
+    await service.connectMilvus({ address: mockAddress }, insightCacheForTest);
     const res = await service.flush({ collection_names: ['c1', 'c2'] });
     const res = await service.flush({ collection_names: ['c1', 'c2'] });
     const data = res.data.collection_names;
     const data = res.data.collection_names;
     expect(data.length).toBe(2);
     expect(data.length).toBe(2);
   });
   });
 
 
   test('test getMetrics method', async () => {
   test('test getMetrics method', async () => {
-    await service.connectMilvus(mockAddress, insightCacheForTest);
+    await service.connectMilvus({ address: mockAddress }, insightCacheForTest);
     const res = await service.getMetrics();
     const res = await service.getMetrics();
     expect(res.type).toBe('system_info');
     expect(res.type).toBe('system_info');
   });
   });

+ 4 - 1
server/src/__tests__/partitions/partitions.service.test.ts

@@ -27,7 +27,10 @@ describe('Test partitions service', () => {
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
 
 
-    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
+    await milvusService.connectMilvus(
+      { address: mockAddress },
+      insightCacheForTest
+    );
     service = new PartitionsService(milvusService);
     service = new PartitionsService(milvusService);
   });
   });
 
 

+ 4 - 1
server/src/__tests__/schema/schema.service.test.ts

@@ -22,7 +22,10 @@ describe('Test schema service', () => {
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeAddress = mockAddress;
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
     MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
 
 
-    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
+    await milvusService.connectMilvus(
+      { address: mockAddress },
+      insightCacheForTest
+    );
     service = new SchemaService(milvusService);
     service = new SchemaService(milvusService);
   });
   });
 
 

+ 5 - 12
server/src/milvus/milvus.service.ts

@@ -41,11 +41,10 @@ export class MilvusService {
 
 
   checkMilvus() {
   checkMilvus() {
     if (!MilvusService.activeMilvusClient) {
     if (!MilvusService.activeMilvusClient) {
-      // todo, need test when activeMilvusClient is empty.
-      throw HttpErrors(HTTP_STATUS_CODE.BAD_REQUEST, {
-        status: 401,
-        message: 'please connect milvus first',
-      });
+      throw HttpErrors(
+        HTTP_STATUS_CODE.FORBIDDEN,
+        'Can not find your connection, please connect Milvus again'
+      );
       // throw new Error('Please connect milvus first');
       // throw new Error('Please connect milvus first');
     }
     }
   }
   }
@@ -85,13 +84,7 @@ export class MilvusService {
 
 
   async checkConnect(address: string, cache: LruCache<any, any>) {
   async checkConnect(address: string, cache: LruCache<any, any>) {
     const milvusAddress = MilvusService.formatAddress(address);
     const milvusAddress = MilvusService.formatAddress(address);
-    if (!cache.has(milvusAddress)) {
-      return { connected: false };
-    }
-    // const res = await this.connectMilvus(address, cache);
-    // return {
-    //   connected: res.address ? true : false,
-    // };
+    return { connected: cache.has(milvusAddress) };
   }
   }
 
 
   async flush(data: FlushReq) {
   async flush(data: FlushReq) {