Răsfoiți Sursa

test: add more thumbnailer tests

KernelDeimos 6 luni în urmă
părinte
comite
7f30ec46e7

+ 90 - 29
src/backend/src/services/thumbnails/HTTPThumbnailService.js

@@ -173,6 +173,10 @@ class HTTPThumbnailService extends BaseService {
     }
 
     checkShouldExec_ () {
+        if ( this.test_mode ) {
+            this.test_checked_exec = true;
+            return;
+        }
         if ( this.status !== this.constructor.STATUS_IDLE ) return;
         if ( this.queue.length === 0 ) return;
         this.exec_();
@@ -237,9 +241,60 @@ class HTTPThumbnailService extends BaseService {
     async exec_0 (queue) {
         const { axios } = this.modules;
 
-        let expected = 0;
+        this.log.info('starting thumbnail request');
+        const resp = await this.request_({ queue });
+        this.log.info('done thumbnail request');
+
+        if ( resp.status !== 200 ) {
+            this.log.error('Thumbnail service returned non-200 status');
+            throw new Error('Thumbnail service returned non-200 status');
+        }
+
+        const results = resp.data;
+
+        this.log.noticeme('response?', { resp });
+        this.log.noticeme('data?', { data: resp.data });
+
+        if ( results.length !== queue.length ) {
+            this.log.error('Thumbnail service returned wrong number of results');
+            throw new Error('Thumbnail service returned wrong number of results');
+        }
+
+        for ( let i = 0 ; i < queue.length ; i++ ) {
+            const result = results[i];
+            const job = queue[i];
+
+            this.log.noticeme('result?', { result });
+            job.resolve(
+                result.encoded
+                && `data:image/png;base64,${result.encoded}`
+            );
+        }
+    }
+
+    async request_ ({ queue }) {
+        if ( this.test_mode ) {
+            const results = [];
+            for ( const job of queue ) {
+                console.log('file?', job.file);
+                if ( job.file?.behavior === 'fail' ) {
+                    throw new Error('test fail');
+                }
+                results.push({
+                    encoded: 'data:image/png;base64,' +
+                        'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX' +
+                        '///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASU' +
+                        'VORK5CYII',
+                });
+            }
+            return {
+                status: 200,
+                data: results,
+            };
+        } 
 
         const form = new FormData();
+        let expected = 0;
         for ( const job of queue ) {
             expected++;
             // const blob = new Blob([job.file.buffer], { type: job.file.mimetype });
@@ -261,7 +316,6 @@ class HTTPThumbnailService extends BaseService {
             });
         }
 
-        this.log.info('starting thumbnail request');
         const resp = await axios.request(
             {
                 method: 'post',
@@ -272,33 +326,8 @@ class HTTPThumbnailService extends BaseService {
                 }
             }
         );
-        this.log.info('done thumbnail request');
-
-        if ( resp.status !== 200 ) {
-            this.log.error('Thumbnail service returned non-200 status');
-            throw new Error('Thumbnail service returned non-200 status');
-        }
-
-        const results = resp.data;
-
-        this.log.noticeme('response?', { resp });
-        this.log.noticeme('data?', { data: resp.data });
-
-        if ( results.length !== queue.length ) {
-            this.log.error('Thumbnail service returned wrong number of results');
-            throw new Error('Thumbnail service returned wrong number of results');
-        }
-
-        for ( let i = 0 ; i < queue.length ; i++ ) {
-            const result = results[i];
-            const job = queue[i];
 
-            this.log.noticeme('result?', { result });
-            job.resolve(
-                result.encoded
-                && `data:image/png;base64,${result.encoded}`
-            );
-        }
+        return resp;
     }
 
     async query_supported_mime_types_() {
@@ -328,7 +357,13 @@ class HTTPThumbnailService extends BaseService {
         this.constructor.SUPPORTED_MIMETYPES = Object.keys(mime_set);
     }
 
-    _test ({ assert }) {
+    async _test ({ assert }) {
+        this.errors.report = () => {};
+        this.log = {
+            info: () => {},
+            error: () => {},
+            noticeme: () => {},
+        };
         // Thumbnail operation eventually recycles
         {
             const thop = new ThumbnailOperation(null);
@@ -337,6 +372,32 @@ class HTTPThumbnailService extends BaseService {
             }
             assert.equal(thop.recycle(), false, 'recycle max');
         }
+
+        this.test_mode = true;
+
+        // Hunch: 
+
+        // Request and await the thumbnailing of a few files
+        for ( let i=0 ; i < 3 ; i++ ) {
+            const job = new ThumbnailOperation({ behavior: 'ok' });
+            this.queue.push(job);
+
+        }
+        this.test_checked_exec = false;
+        await this.exec_();
+        assert.equal(this.queue.length, 0, 'queue emptied');
+        assert.equal(this.test_checked_exec, true, 'checked exec');
+
+
+        // test with failed job
+        const job = new ThumbnailOperation({ behavior: 'fail' });
+        this.queue.push(job);
+        this.test_checked_exec = false;
+        await this.exec_();
+        assert.equal(this.queue.length, 0, 'queue emptied');
+        assert.equal(this.test_checked_exec, true, 'checked exec');
+
+        this.test_mode = false;
     }
 }
 

+ 1 - 1
src/backend/tools/test.js

@@ -208,7 +208,7 @@ const main = async () => {
             }
         };
 
-        ins._test(testapi);
+        await ins._test(testapi);
 
         total_passed += passed;
         total_failed += failed;