@@ -9,7 +9,6 @@ import path from 'path';
99import fs from 'fs' ;
1010import git from 'isomorphic-git' ;
1111import { EncryptedFS } from 'encryptedfs' ;
12- import { timedCancellable as timedCancellableF } from '@matrixai/contexts/dist/functions' ;
1312import { DB } from '@matrixai/db' ;
1413import { withF } from '@matrixai/resources' ;
1514import Logger , { LogLevel , StreamHandler } from '@matrixai/logger' ;
@@ -641,62 +640,59 @@ describe('VaultInternal', () => {
641640 await expect ( vault . version ( newRef2 ) ) . rejects . toThrow ( ) ;
642641 } ) ;
643642 test ( 'commit added if mutation in writeG' , async ( ) => {
644- const f = async ( ctx : ContextTimed ) => {
645- const commit = ( await vault . log ( ) ) [ 0 ] . commitId ;
646- const gen = vault . writeG (
647- async function * ( efs ) : AsyncGenerator {
648- yield await efs . writeFile ( 'secret-1' , 'secret-content' ) ;
649- } ,
650- undefined ,
651- ctx ,
652- ) ;
653- for await ( const _ of gen ) {
654- // Do nothing
655- }
656- const log = await vault . log ( ) ;
657- expect ( log ) . toHaveLength ( 2 ) ;
658- expect ( log [ 0 ] . commitId ) . not . toStrictEqual ( commit ) ;
659- } ;
660- await timedCancellableF ( f , true ) ( ) ;
643+ const abortController = new AbortController ( ) ;
644+ const ctx = { signal : abortController . signal } as ContextTimed ;
645+ const commit = ( await vault . log ( ) ) [ 0 ] . commitId ;
646+ const gen = vault . writeG (
647+ async function * ( efs ) : AsyncGenerator {
648+ yield await efs . writeFile ( 'secret-1' , 'secret-content' ) ;
649+ } ,
650+ undefined ,
651+ ctx ,
652+ ) ;
653+ for await ( const _ of gen ) {
654+ // Do nothing
655+ }
656+ const log = await vault . log ( ) ;
657+ expect ( log ) . toHaveLength ( 2 ) ;
658+ expect ( log [ 0 ] . commitId ) . not . toStrictEqual ( commit ) ;
661659 } ) ;
662660 test ( 'no commit added if no mutation in writeG' , async ( ) => {
663- const f = async ( ctx : ContextTimed ) => {
664- const commit = ( await vault . log ( ) ) [ 0 ] . commitId ;
665- const gen = vault . writeG (
666- async function * ( _efs ) : AsyncGenerator { } ,
667- undefined ,
668- ctx ,
669- ) ;
670- for await ( const _ of gen ) {
671- // Do nothing
672- }
673- const log = await vault . log ( ) ;
674- expect ( log ) . toHaveLength ( 1 ) ;
675- expect ( log [ 0 ] . message ) . not . toContain ( 'secret-1' ) ;
676- expect ( log [ 0 ] . commitId ) . toStrictEqual ( commit ) ;
677- } ;
678- await timedCancellableF ( f , true ) ( ) ;
661+ const abortController = new AbortController ( ) ;
662+ const ctx = { signal : abortController . signal } as ContextTimed ;
663+ const commit = ( await vault . log ( ) ) [ 0 ] . commitId ;
664+ const gen = vault . writeG (
665+ async function * ( _efs ) : AsyncGenerator { } ,
666+ undefined ,
667+ ctx ,
668+ ) ;
669+ for await ( const _ of gen ) {
670+ // Do nothing
671+ }
672+ const log = await vault . log ( ) ;
673+ expect ( log ) . toHaveLength ( 1 ) ;
674+ expect ( log [ 0 ] . message ) . not . toContain ( 'secret-1' ) ;
675+ expect ( log [ 0 ] . commitId ) . toStrictEqual ( commit ) ;
679676 } ) ;
680677 test ( 'no mutation to vault when part of a commit operation fails in writeG' , async ( ) => {
681- const f = async ( ctx : ContextTimed ) => {
682- const gen = vault . writeG (
683- async function * ( efs ) : AsyncGenerator {
684- yield await efs . writeFile ( secret1 . name , secret1 . content ) ;
685- yield await efs . rename ( 'notValid' , 'randomName' ) ; // Throws
686- } ,
687- undefined ,
688- ctx ,
689- ) ;
690- // Failing commit operation
691- await expect ( ( ) => consumeGenerator ( gen ) ) . rejects . toThrow ( ) ;
692- // Make sure secret1 wasn't written when the above commit failed
693- await vault . readF ( async ( efs ) => {
694- expect ( await efs . readdir ( '.' ) ) . not . toContain ( secret1 . name ) ;
695- } ) ;
696- // No new commit
697- expect ( await vault . log ( ) ) . toHaveLength ( 1 ) ;
698- } ;
699- await timedCancellableF ( f , true ) ( ) ;
678+ const abortController = new AbortController ( ) ;
679+ const ctx = { signal : abortController . signal } as ContextTimed ;
680+ const gen = vault . writeG (
681+ async function * ( efs ) : AsyncGenerator {
682+ yield await efs . writeFile ( secret1 . name , secret1 . content ) ;
683+ yield await efs . rename ( 'notValid' , 'randomName' ) ; // Throws
684+ } ,
685+ undefined ,
686+ ctx ,
687+ ) ;
688+ // Failing commit operation
689+ await expect ( ( ) => consumeGenerator ( gen ) ) . rejects . toThrow ( ) ;
690+ // Make sure secret1 wasn't written when the above commit failed
691+ await vault . readF ( async ( efs ) => {
692+ expect ( await efs . readdir ( '.' ) ) . not . toContain ( secret1 . name ) ;
693+ } ) ;
694+ // No new commit
695+ expect ( await vault . log ( ) ) . toHaveLength ( 1 ) ;
700696 } ) ;
701697 test ( 'no commit after readG' , async ( ) => {
702698 await vault . writeF ( async ( efs ) => {
@@ -757,7 +753,7 @@ describe('VaultInternal', () => {
757753 refs . push ( await quickCommit ( logElement . commitId , `secret-${ num ++ } ` ) ) ;
758754 }
759755 const abortController = new AbortController ( ) ;
760- const ctx = { signal : abortController . signal } ;
756+ const ctx = { signal : abortController . signal } as ContextTimed ;
761757 // @ts -ignore: protected method
762758 await vault . garbageCollectGitObjectsGlobal ( ctx ) ;
763759
@@ -802,46 +798,45 @@ describe('VaultInternal', () => {
802798 expect ( finished ) . toBe ( true ) ;
803799 } ) ;
804800 test ( 'writeG respects read and write locking' , async ( ) => {
805- const f = async ( ctx : ContextTimed ) => {
806- // Hold a write lock
807- const lock = vault . getLock ( ) ;
808- const [ releaseWrite ] = await lock . write ( ) ( ) ;
801+ const abortController = new AbortController ( ) ;
802+ const ctx = { signal : abortController . signal } as ContextTimed ;
803+ // Hold a write lock
804+ const lock = vault . getLock ( ) ;
805+ const [ releaseWrite ] = await lock . write ( ) ( ) ;
809806
810- let finished = false ;
811- const writeGen = vault . writeG (
812- async function * ( ) {
813- yield ;
814- finished = true ;
815- yield ;
816- } ,
817- undefined ,
818- ctx ,
819- ) ;
820- const runP = consumeGenerator ( writeGen ) ;
821- await sleep ( waitDelay ) ;
822- expect ( finished ) . toBe ( false ) ;
823- await releaseWrite ( ) ;
824- await runP ;
825- expect ( finished ) . toBe ( true ) ;
807+ let finished = false ;
808+ const writeGen = vault . writeG (
809+ async function * ( ) {
810+ yield ;
811+ finished = true ;
812+ yield ;
813+ } ,
814+ undefined ,
815+ ctx ,
816+ ) ;
817+ const runP = consumeGenerator ( writeGen ) ;
818+ await sleep ( waitDelay ) ;
819+ expect ( finished ) . toBe ( false ) ;
820+ await releaseWrite ( ) ;
821+ await runP ;
822+ expect ( finished ) . toBe ( true ) ;
826823
827- const [ releaseRead ] = await lock . read ( ) ( ) ;
828- finished = false ;
829- const writeGen2 = vault . writeG (
830- async function * ( ) {
831- yield ;
832- finished = true ;
833- yield ;
834- } ,
835- undefined ,
836- ctx ,
837- ) ;
838- const runP2 = consumeGenerator ( writeGen2 ) ;
839- await sleep ( waitDelay ) ;
840- await releaseRead ( ) ;
841- await runP2 ;
842- expect ( finished ) . toBe ( true ) ;
843- } ;
844- await timedCancellableF ( f , true ) ( ) ;
824+ const [ releaseRead ] = await lock . read ( ) ( ) ;
825+ finished = false ;
826+ const writeGen2 = vault . writeG (
827+ async function * ( ) {
828+ yield ;
829+ finished = true ;
830+ yield ;
831+ } ,
832+ undefined ,
833+ ctx ,
834+ ) ;
835+ const runP2 = consumeGenerator ( writeGen2 ) ;
836+ await sleep ( waitDelay ) ;
837+ await releaseRead ( ) ;
838+ await runP2 ;
839+ expect ( finished ) . toBe ( true ) ;
845840 } ) ;
846841 test ( 'readF respects write locking' , async ( ) => {
847842 const lock = vault . getLock ( ) ;
@@ -952,49 +947,46 @@ describe('VaultInternal', () => {
952947 await releaseRead ( ) ;
953948 } ) ;
954949 test ( 'can acquire a write resource' , async ( ) => {
955- const f = async ( ctx : ContextTimed ) => {
956- const acquireWrite = vault . acquireWrite ( undefined , ctx ) ;
957- await withF ( [ acquireWrite ] , async ( [ efs ] ) => {
958- await efs . writeFile ( secret1 . name , secret1 . content ) ;
959- } ) ;
960- await vault . readF ( async ( efs ) => {
961- const content = await efs . readFile ( secret1 . name ) ;
962- expect ( content . toString ( ) ) . toEqual ( secret1 . content ) ;
963- } ) ;
964- } ;
965- await timedCancellableF ( f , true ) ( ) ;
950+ const abortController = new AbortController ( ) ;
951+ const ctx = { signal : abortController . signal } as ContextTimed ;
952+ const acquireWrite = vault . acquireWrite ( ctx ) ;
953+ await withF ( [ acquireWrite ] , async ( [ efs ] ) => {
954+ await efs . writeFile ( secret1 . name , secret1 . content ) ;
955+ } ) ;
956+ await vault . readF ( async ( efs ) => {
957+ const content = await efs . readFile ( secret1 . name ) ;
958+ expect ( content . toString ( ) ) . toEqual ( secret1 . content ) ;
959+ } ) ;
966960 } ) ;
967961 test ( 'acquiring write resource respects write locking' , async ( ) => {
968- const f = async ( ctx : ContextTimed ) => {
969- const lock = vault . getLock ( ) ;
970- const [ releaseWrite ] = await lock . write ( ) ( ) ;
971- let finished = false ;
972- const writeP = withF ( [ vault . acquireWrite ( undefined , ctx ) ] , async ( ) => {
973- finished = true ;
974- } ) ;
975- await sleep ( waitDelay ) ;
976- expect ( finished ) . toBe ( false ) ;
977- await releaseWrite ( ) ;
978- await writeP ;
979- expect ( finished ) . toBe ( true ) ;
980- } ;
981- await timedCancellableF ( f , true ) ( ) ;
962+ const abortController = new AbortController ( ) ;
963+ const ctx = { signal : abortController . signal } as ContextTimed ;
964+ const lock = vault . getLock ( ) ;
965+ const [ releaseWrite ] = await lock . write ( ) ( ) ;
966+ let finished = false ;
967+ const writeP = withF ( [ vault . acquireWrite ( ctx ) ] , async ( ) => {
968+ finished = true ;
969+ } ) ;
970+ await sleep ( waitDelay ) ;
971+ expect ( finished ) . toBe ( false ) ;
972+ await releaseWrite ( ) ;
973+ await writeP ;
974+ expect ( finished ) . toBe ( true ) ;
982975 } ) ;
983976 test ( 'acquiring write resource respects read locking' , async ( ) => {
984- const f = async ( ctx : ContextTimed ) => {
985- const lock = vault . getLock ( ) ;
986- const [ releaseRead ] = await lock . read ( ) ( ) ;
987- let finished = false ;
988- const writeP = withF ( [ vault . acquireWrite ( undefined , ctx ) ] , async ( ) => {
989- finished = true ;
990- } ) ;
991- await sleep ( waitDelay ) ;
992- expect ( finished ) . toBe ( false ) ;
993- await releaseRead ( ) ;
994- await writeP ;
995- expect ( finished ) . toBe ( true ) ;
996- } ;
997- await timedCancellableF ( f , true ) ( ) ;
977+ const abortController = new AbortController ( ) ;
978+ const ctx = { signal : abortController . signal } as ContextTimed ;
979+ const lock = vault . getLock ( ) ;
980+ const [ releaseRead ] = await lock . read ( ) ( ) ;
981+ let finished = false ;
982+ const writeP = withF ( [ vault . acquireWrite ( ctx ) ] , async ( ) => {
983+ finished = true ;
984+ } ) ;
985+ await sleep ( waitDelay ) ;
986+ expect ( finished ) . toBe ( false ) ;
987+ await releaseRead ( ) ;
988+ await writeP ;
989+ expect ( finished ) . toBe ( true ) ;
998990 } ) ;
999991 // Life-cycle
1000992 test ( 'can create with CreateVaultInternal' , async ( ) => {
0 commit comments