Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Unified Diff: third_party/WebKit/LayoutTests/external/wpt/IndexedDB/interleaved-cursors.html

Issue 2763813002: Ensure correctness for interleaved IndexedDB cursor iteration. (Closed)
Patch Set: Relax quota restriction for the test to pass. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/external/wpt/IndexedDB/interleaved-cursors.html
diff --git a/third_party/WebKit/LayoutTests/external/wpt/IndexedDB/interleaved-cursors.html b/third_party/WebKit/LayoutTests/external/wpt/IndexedDB/interleaved-cursors.html
new file mode 100644
index 0000000000000000000000000000000000000000..0991337d5d22aa45ad371a638f2f8cec39d3e89d
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/external/wpt/IndexedDB/interleaved-cursors.html
@@ -0,0 +1,177 @@
+<!doctype html>
+<meta charset="utf-8">
+<meta name="timeout" content="long">
+<title>IndexedDB: Interleaved iteration of multiple cursors</title>
+<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="support-promises.js"></script>
+<script>
+// Number of objects that each iterator goes over.
+const itemCount = 10;
+
+// Ratio of small objects to large objects.
+const largeObjectRatio = 5;
+
+// Size of large objects. This should exceed the size of a block in the storage
+// method underlying the browser's IndexedDB implementation. For example, this
+// needs to exceed the LevelDB block size on Chrome, and the SQLite block size
+// on Firefox.
+const largeObjectSize = 48 * 1024;
+
+function objectKey(cursorIndex, itemIndex) {
+ return `${cursorIndex}-key-${itemIndex}`;
+}
+
+function objectValue(cursorIndex, itemIndex) {
+ if ((cursorIndex * itemCount + itemIndex) % largeObjectRatio === 0) {
+ // We use a typed array (as opposed to a string) because IndexedDB
+ // implementations may serialize strings using UTF-8 or UTF-16, yielding
+ // larger IndexedDB entries than we'd expect. It's very unlikely that an
+ // IndexedDB implementation would use anything other than the raw buffer to
+ // serialize a typed array.
+ const buffer = new Uint8Array(largeObjectSize);
+
+ // Some IndexedDB implementations, like LevelDB, compress their data blocks
+ // before storing them to disk. We use a simple 32-bit xorshift PRNG, which
+ // should be sufficient to foil any fast generic-purpose compression scheme.
+
+ // 32-bit xorshift - the seed can't be zero
+ let state = 1000 + (cursorIndex * itemCount + itemIndex);
+
+ for (let i = 0; i < largeObjectSize; ++i) {
+ state ^= state << 13;
+ state ^= state >> 17;
+ state ^= state << 5;
+ buffer[i] = state & 0xff;
+ }
+
+ return buffer;
+ }
+ return [cursorIndex, 'small', itemIndex];
+}
+
+
+// Writes the objects to be read by one cursor. Returns a promise that resolves
+// when the write completes.
+//
+// We want to avoid creating a large transaction, because that is outside the
+// test's scope, and it's a bad practice. So we break up the writes across
+// multiple transactions. For simplicity, each transaction writes all the
+// objects that will be read by a cursor.
+function writeCursorObjects(database, cursorIndex) {
+ return new Promise((resolve, reject) => {
+ const transaction = database.transaction('cache', 'readwrite');
+ const store = transaction.objectStore('cache');
+ for (let i = 0; i < itemCount; ++i) {
+ store.put({
+ key: objectKey(cursorIndex, i), value: objectValue(cursorIndex, i)});
+ }
+ transaction.oncomplete = resolve;
+ transaction.onerror = () => { reject(transaction.error); };
jsbell 2017/03/23 21:20:01 transaction.error won't be set to anything here si
pwnall 2017/03/23 22:08:27 Done.
+ transaction.onabort = () => { reject(transaction.error); };
+ });
+}
+
+// Returns a promise that resolves when the store has been populated.
+function populateTestStore(testCase, database, cursorCount) {
+ let promiseChain = Promise.resolve();
+
+ for (let i = 0; i < cursorCount; ++i)
+ promiseChain = promiseChain.then(() => writeCursorObjects(database, i));
+
+ return promiseChain;
+}
+
+function interleaveCursors(testCase, store, cursorCount) {
+ return new Promise((resolve, reject) => {
+ const cursors = [];
+ const requests = [];
dmurph 2017/03/23 21:10:52 Can you add a comment like the following: // Reque
pwnall 2017/03/23 22:08:27 Done.
+
+ const checkCursorState = (cursorIndex, itemIndex) => {
+ const cursor = cursors[cursorIndex];
+ assert_equals(cursor.key, objectKey(cursorIndex, itemIndex));
+ assert_equals(cursor.value.key, objectKey(cursorIndex, itemIndex));
+ assert_equals(
+ cursor.value.value.join('-'),
+ objectValue(cursorIndex, itemIndex).join('-'));
+ };
+
+ const openCursor = (cursorIndex, callback) => {
+ const request = store.openCursor(
+ IDBKeyRange.lowerBound(objectKey(cursorIndex, 0)));
+ requests[cursorIndex] = request;
+
+ request.onsuccess = testCase.step_func(() => {
+ const cursor = request.result;
+ cursors[cursorIndex] = cursor;
+ checkCursorState(cursorIndex, 0);
+ callback();
+ });
+ request.onerror = event => reject(request.error);
+ };
+
+ const readItemFromCursor = (cursorIndex, itemIndex, callback) => {
+ const request = requests[cursorIndex];
+ request.onsuccess = testCase.step_func(() => {
+ const cursor = request.result;
+ cursors[cursorIndex] = cursor;
+ checkCursorState(cursorIndex, itemIndex);
+ callback();
+ });
+
+ const cursor = cursors[cursorIndex];
+ cursor.continue();
+ };
+
+ // We open all the cursors one at a time, then cycle through the cursors and
+ // call continue() on each of them. This access pattern causes maximal
+ // trashing to an LRU cursor cache. Eviction scheme aside, any cache will
+ // have to evict some cursors, and this access pattern verifies that the
+ // cache correctly restores the state of evicted cursors.
+ const steps = [];
+ for (let cursorIndex = 0; cursorIndex < cursorCount; ++cursorIndex)
+ steps.push(openCursor.bind(null, cursorIndex));
+ for (let itemIndex = 1; itemIndex < itemCount; ++itemIndex) {
+ for (let cursorIndex = 0; cursorIndex < cursorCount; ++cursorIndex)
+ steps.push(readItemFromCursor.bind(null, cursorIndex, itemIndex));
+ }
+
+ const runStep = (stepIndex) => {
+ if (stepIndex === steps.length) {
+ resolve();
+ return;
+ }
+ steps[stepIndex](() => { runStep(stepIndex + 1); });
+ };
+ runStep(0);
+ });
+}
+
+for (let cursorCount of [1, 10, 100, 500]) {
+ promise_test(testCase => {
+ return createDatabase(testCase, (database, transaction) => {
+ const store = database.createObjectStore('cache',
+ { keyPath: 'key', autoIncrement: true });
+ }).then(database => {
+ return populateTestStore(testCase, database, cursorCount).then(
+ () => database);
+ }).then(database => {
+ database.close();
+ }).then(() => {
+ return openDatabase(testCase);
+ }).then(database => {
+ const transaction = database.transaction('cache', 'readonly');
+ const store = transaction.objectStore('cache');
+ return interleaveCursors(testCase, store, cursorCount).then(
jsbell 2017/03/23 21:20:01 This return means the transaction.onXXX below won'
pwnall 2017/03/23 22:08:27 Done. Durr! Thanks for catching that!
+ () => database);
+
+ transaction.onerror = () => { reject(transaction.error); };
jsbell 2017/03/23 21:20:01 Same as above - don't bother with transaction.oner
pwnall 2017/03/23 22:08:27 Done.
+ transaction.onabort = () => { reject(transaction.error); };
+ }).then(database => {
+ database.close();
+ indexedDB.deleteDatabase(database.name);
+ });
+ }, `${cursorCount} cursors`);
+}
+</script>

Powered by Google App Engine
This is Rietveld 408576698