import assert from 'assert' ; import { describe , it , before , after } from 'mocha' ; import nock from 'nock' ; import DataStoreEmulator from 'google-datastore-emulator' ; import { DatastoreLock } from '../src/datastore-lock' ; describe ( 'datastore-lock' , ( ) => { let emulator ; before ( ( ) => { nock . enableNetConnect ( '127.0.0.1' ) ; nock . enableNetConnect ( 'localhost' ) ; const options = { useDocker : true , } ; emulator = new DataStoreEmulator ( options ) ; return emulator . start ( ) ; } ) ; after ( ( ) => { emulator . stop ( ) ; nock . disableNetConnect ( ) ; } ) ; describe ( 'lock test' , ( ) => { it ( 'successfully acquires and releases the lock' , async ( ) => { const l = new DatastoreLock ( 'datastore-lock-test' , 'test' , 500 ) ; const l2 = new DatastoreLock ( 'datastore-lock-test' , 'test' , 0 ) ; const l3 = new DatastoreLock ( 'datastore-lock-test' , 'test' , 0 ) ; assert ( await l . acquire ( ) ) ; assert ( await l2 . acquire ( ) ) ; assert ( await l3 . acquire ( ) ) ; await assert . rejects ( l . release ( ) , Error ) ; await assert . rejects ( l2 . release ( ) , Error ) ; assert ( await l3 . release ( ) ) ; } ) ; it ( 'fails for acquiring and releasing the lock' , async ( ) => { const l = new DatastoreLock ( 'xxxxx' . repeat ( 1024 ) , 'test' , 0 , 50 ) ; assert ( ! ( await l . acquire ( ) ) ) ; assert ( ! ( await l . release ( ) ) ) ; assert . throws ( ( ) => { new DatastoreLock ( 'datastore-lock-test' , 'test' , 120 * 1000 + 1 ) ; } , Error ) ; } ) ; } ) ; } ) ;