summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/FileSystemFileHandle-writable-file-stream-lock-modes.https.tentative.worker.js
blob: 3e9b4334b9a4d512c42f8056b1bc95aa22ac0920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
importScripts('/resources/testharness.js');
importScripts('resources/sandboxed-fs-test-helpers.js');
importScripts('resources/test-helpers.js');

'use strict';

// Adds tests for expected behaviors of a writable stream created in `wfsMode`
// mode.
function lockPropertyTests(wfsMode, expectedLockAccess) {
  const createWFSLock = createWFSWithCleanupFactory({mode: wfsMode});

  directory_test(async (t, rootDir) => {
    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');

    const {mode} = await createWFSLock(t, fileHandle);
    assert_equals(mode, wfsMode);
  }, `A writable stream in ${wfsMode} mode has a mode property equal to` +
    ` ${wfsMode}`);

  directory_test(async (t, rootDir) => {
    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
    assert_equals(
        await testLockAccess(t, fileHandle, createWFSLock), expectedLockAccess);
  }, `A writable stream in ${wfsMode} mode takes a lock that is` +
    ` ${expectedLockAccess}`);

  // Test interaction with other writable stream modes.
  for (const mode of WFS_MODES) {
    // Add tests depending on which writable stream modes are being tested
    // against each other.
    const testingAgainstSelf = mode === wfsMode;
    const testingExclusiveLock = expectedLockAccess === 'exclusive';
    const tests = {
      diffFile: `When there's an open writable stream in ${wfsMode} mode on a` +
          ` file, can open another writable stream in ${mode} on a different` +
          ` file`,
    };
    if (!testingAgainstSelf || testingExclusiveLock) {
      tests.sameFile = `When there's an open writable stream in ${wfsMode}` +
          ` mode on a file, cannot open another writable stream in ${mode} on` +
          ` that same file`;
    }
    if (testingExclusiveLock) {
      tests.acquireAfterRelease = `After a writable stream in ${wfsMode} mode` +
          ` on a file has been closed, can open another writable stream in` +
          ` ${mode} on the same file`;
    }
    if (!testingExclusiveLock && !testingAgainstSelf) {
      tests.multiAcquireAfterRelease = `After all writable streams in` +
          ` ${wfsMode} mode on a file has been closed, can open another` +
          ` writable stream in ${mode} on the same file`;
    }

    generateCrossLockTests(
        createWFSLock, createWFSWithCleanupFactory({mode: mode}), tests);
  }
}

directory_test(async (t, rootDir) => {
  const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');

  const syncHandle = await createWFSWithCleanup(t, fileHandle);
  assert_equals(syncHandle.mode, 'siloed');
}, 'A writable stream opens in siloed mode by default');

lockPropertyTests('siloed', LOCK_ACCESS.SHARED);
lockPropertyTests('exclusive', LOCK_ACCESS.EXCLUSIVE);

done();