Ver código fonte

storage-node: fix test, ensure write stream is flushed before committing

Mokhtar Naamani 4 anos atrás
pai
commit
f64066776e
1 arquivos alterados com 32 adições e 32 exclusões
  1. 32 32
      storage-node/packages/storage/test/storage.js

+ 32 - 32
storage-node/packages/storage/test/storage.js

@@ -40,8 +40,11 @@ function write(store, content_id, contents, callback)
       });
       stream.on('committed', callback);
 
-      stream.write(contents);
-      stream.end();
+      if (!stream.write(contents)) {
+        stream.once('drain', () => stream.end())
+      } else {
+        process.nextTick(() => stream.end())
+      }
     })
     .catch((err) => {
       expect.fail(err);
@@ -92,38 +95,35 @@ describe('storage/storage', () => {
 
     it('detects the MIME type of a write stream', (done) => {
       const contents = fs.readFileSync('../../storage-node_new.svg');
+      storage.open('mime-test', 'w')
+        .then((stream) => {
+          var file_info;
+          stream.on('file_info', (info) => {
+            // Could filter & abort here now, but we're just going to set this,
+            // and expect it to be set later...
+            file_info = info;
+          });
 
-      create_known_object('foobar', contents, (store, hash) => {
-        var file_info;
-        store.open('mime-test', 'w')
-          .then((stream) => {
-
-            stream.on('file_info', (info) => {
-              // Could filter & abort here now, but we're just going to set this,
-              // and expect it to be set later...
-              file_info = info;
-            });
-
-            stream.on('finish', () => {
-              stream.commit();
-            });
-
-            stream.on('committed', (hash) => {
-              // ... if file_info is not set here, there's an issue.
-              expect(file_info).to.have.property('mime_type', 'application/xml');
-              expect(file_info).to.have.property('ext', 'xml');
-
-              done();
-            });
-
-            stream.write(contents);
-            stream.end();
-          })
-          .catch((err) => {
-            expect.fail(err);
+          stream.on('finish', () => {
+            stream.commit();
           });
-      });
 
+          stream.on('committed', (hash) => {
+            // ... if file_info is not set here, there's an issue.
+            expect(file_info).to.have.property('mime_type', 'application/xml');
+            expect(file_info).to.have.property('ext', 'xml');
+            done();
+          });
+
+          if (!stream.write(contents)) {
+            stream.once('drain', () => stream.end())
+          } else {
+            process.nextTick(() => stream.end())
+          }
+        })
+        .catch((err) => {
+          expect.fail(err);
+        });
     });
 
     it('can read a stream', (done) => {
@@ -196,7 +196,7 @@ describe('storage/storage', () => {
     it('returns stats for a known object', (done) => {
       const content = 'stat-test';
       const expected_size = content.length;
-      create_known_object('foobar', 'stat-test', (store, hash) => {
+      create_known_object('foobar', content, (store, hash) => {
         expect(store.stat(hash)).to.eventually.have.property('size', expected_size);
         done();
       });