<commit_before>///<reference path="qunit.js" />
///<reference path="../src/Dexie.js" />
(function () {
    var db = new Dexie("TestDB");
    db.version(1).stores({
        users: "username",
        pets: "++id,kind",
        petsPerUser: "++,user,pet"
    });

    module("transaction", {
        setup: function () {
            stop();
            db.delete().then(function () {
                db.open();
                start();
            }).catch(function (e) {
                ok(false, "Error deleting database: " + e);
                start();
            });
        },
        teardown: function () {
            stop(); db.delete().finally(start);
        }
    });

    asyncTest("empty transaction block", function () {
        db.transaction('rw', db.users, db.pets, function () {
            ok(true, "Entering transaction block but dont start any transaction");
            // Leave it empty. 
        }).catch(function (err) {
            ok(false, err);
        }).finally(function () {
            setTimeout(start, 10);
        });
    });

    asyncTest("db.transaction()", function () {
        db.transaction('rw', db.users, function () {
            db.users.add({ username: "arne" });
            return db.users.get("arne", function (user) {
                equal(user.username, "arne", "Got user arne the line after adding it - we must be in a transaction");
                ok(Dexie.currentTransaction != null, "Current Transaction must be set");
            });
        }).then(function () {
            ok(Dexie.currentTransaction == null, "Current Transaction must be null even when transaction scope returned a Promise that was bound to the transaction");
        }).finally(start);
    });

    asyncTest("Table not in transaction", function () {
        db.pets.add({kind: "dog"}).then(function() {
            return db.transaction('rw', db.users, function () {
                db.users.add({ username: "arne" });
                return db.pets.get(1, function (pet) {
                    ok(false, "Should not be able to get a pet because pets is not in transaction");
                });
            }).then(function () {
                ok(false, "Transaction should not commit because I made an error");
            }).catch(function (err) {
                ok(true, "Got error since we tried using a table not in transaction: " + err);
            });
        }).finally(start);
    });

    asyncTest("Table not in transaction 2", function () {
      return db.transaction('rw', db.users, function () {
        db.pets.add({kind: "dog"});
      }).then(function () {
        ok(false, "Transaction should not commit because I made an error");
      }).catch(function (err) {
        ok(true, "Got error since we tried using a table not in transaction: " + err);
      }).finally(start);
    });

    asyncTest("Write into readonly transaction", function () {
        return db.transaction('r', db.users, function () {
            db.users.add({ username: "arne" }).then(function(){
                ok(false, "Should not be able to get a here because we tried to write to users when in a readonly transaction");
            });
        }).then(function () {
            ok(false, "Transaction should not commit because I made an error");
        }).catch(function (err) {
            ok(true, "Got error since we tried to write to users when in a readonly transaction: " + err);
        }).finally(start);
    });

    asyncTest("Inactive transaction", function () {
        return db.transaction('rw', db.users, function () {
            return new Dexie.Promise(function (resolve, reject) {
                // Wait a little with resolving this custom promise.... (so that IDB framework must commit the transaction)
                setTimeout(resolve, 100);

                // Notify log when transaction completes too early
                Dexie.currentTransaction.complete(function () {
                    ok(true, "Transaction committing too early...");
                });

            }).then(function () {
                // Now when transaction has already committed, try to add a user with the current transaction:
                return db.users.add({ username: "arne" });
            }).then(function () {
                ok(false, "Should not be able to get a here transaction has become inactive");
            });
        }).then(function () {
            ok(false, "Should not be able to get a here transaction has become inactive");
        }).catch(function (err) {
            ok(true, "Got error because the transaction has already committed: " + err);
        }).finally(start);
    });

    asyncTest("Inactive transaction 2", function () {
        return db.transaction('rw', db.users, function () {
            // First make an operation so that transaction is internally created (this is the thing differing from the previous test case
            return db.users.add({ username: "arne" }).then(function () {

                // Create a custom promise that will use setTimeout() so that IDB transaction will commit
                return new Dexie.Promise(function (resolve, reject) {
                    // Wait a little with resolving this custom promise.... (so that IDB framework must commit the transaction)
                    setTimeout(resolve, 100);

                    // Notify log when transaction completes too early
                    Dexie.currentTransaction.complete(function () {
                        ok(true, "Transaction committing too early...");
                    })
                });
            }).then(function () {
                // Now when transaction has already committed, try to add a user with the current transaction:
                return db.users.add({ username: "arne" });
            }).then(function () {
                ok(false, "Should not be able to get a here transaction has become inactive");
            });
        }).then(function () {
            ok(false, "Should not be able to get a here transaction has become inactive");
        }).catch(function (err) {
            ok(true, "Got error because the transaction has already committed: " + err);
        }).finally(start);
    });

    asyncTest("sub-transactions", function () {
        var parentTrans;

        function addUser(user, pets) {
            return db.transaction('rw', db.users, db.pets, db.petsPerUser, function () {
                ok(parentTrans._reculock > 0, "Parent transaction is locked");
                db.users.add(user);<commit_msg>Prohibited race condition in 2 unit tests<commit_after>///<reference path="qunit.js" />
///<reference path="../src/Dexie.js" />
(function () {
    var db = new Dexie("TestDB");
    db.version(1).stores({
        users: "username",
        pets: "++id,kind",
        petsPerUser: "++,user,pet"
    });

    module("transaction", {
        setup: function () {
            stop();
            db.delete().then(function () {
                db.open();
                start();
            }).catch(function (e) {
                ok(false, "Error deleting database: " + e);
                start();
            });
        },
        teardown: function () {
            stop(); db.delete().finally(start);
        }
    });

    asyncTest("empty transaction block", function () {
        db.transaction('rw', db.users, db.pets, function () {
            ok(true, "Entering transaction block but dont start any transaction");
            // Leave it empty. 
        }).catch(function (err) {
            ok(false, err);
        }).finally(function () {
            setTimeout(start, 10);
        });
    });

    asyncTest("db.transaction()", function () {
        db.transaction('rw', db.users, function () {
            db.users.add({ username: "arne" });
            return db.users.get("arne", function (user) {
                equal(user.username, "arne", "Got user arne the line after adding it - we must be in a transaction");
                ok(Dexie.currentTransaction != null, "Current Transaction must be set");
            });
        }).then(function () {
            ok(Dexie.currentTransaction == null, "Current Transaction must be null even when transaction scope returned a Promise that was bound to the transaction");
        }).finally(start);
    });

    asyncTest("Table not in transaction", function () {
        db.pets.add({kind: "dog"}).then(function() {
            return db.transaction('rw', db.users, function () {
                db.users.add({ username: "arne" });
                return db.pets.get(1, function (pet) {
                    ok(false, "Should not be able to get a pet because pets is not in transaction");
                });
            }).then(function () {
                ok(false, "Transaction should not commit because I made an error");
            }).catch(function (err) {
                ok(true, "Got error since we tried using a table not in transaction: " + err);
            });
        }).finally(start);
    });

    asyncTest("Table not in transaction 2", function () {
      return db.transaction('rw', db.users, function () {
        db.pets.add({kind: "dog"});
      }).then(function () {
        ok(false, "Transaction should not commit because I made an error");
      }).catch(function (err) {
        ok(true, "Got error since we tried using a table not in transaction: " + err);
      }).finally(start);
    });

    asyncTest("Write into readonly transaction", function () {
        return db.transaction('r', db.users, function () {
            db.users.add({ username: "arne" }).then(function(){
                ok(false, "Should not be able to get a here because we tried to write to users when in a readonly transaction");
            });
        }).then(function () {
            ok(false, "Transaction should not commit because I made an error");
        }).catch(function (err) {
            ok(true, "Got error since we tried to write to users when in a readonly transaction: " + err);
        }).finally(start);
    });

    asyncTest("Inactive transaction", function () {
        return db.transaction('rw', db.users, function () {
            return new Dexie.Promise(function (resolve, reject) {

                // Notify log when transaction completes too early
                Dexie.currentTransaction.complete(function () {
                    ok(true, "Transaction committing too early...");
                    // Resolve the promise after transaction commit.
                    // Flow will continue in the same Transaction scope but with an
                    // inactive transaction
                    resolve();
                });

            }).then(function () {
                // Now when transaction has already committed, try to add a user with the current transaction:
                return db.users.add({ username: "arne" });
            }).then(function () {
                ok(false, "Should not be able to get a here transaction has become inactive");
            });
        }).then(function () {
            ok(false, "Should not be able to get a here transaction has become inactive");
        }).catch(function (err) {
            ok(true, "Got error because the transaction has already committed: " + err);
        }).finally(start);
    });

    asyncTest("Inactive transaction 2", function () {
        return db.transaction('rw', db.users, function () {
            // First make an operation so that transaction is internally created (this is the thing differing from the previous test case
            return db.users.add({ username: "arne" }).then(function () {

                // Create a custom promise that will use setTimeout() so that IDB transaction will commit
                return new Dexie.Promise(function (resolve, reject) {
                    // Notify log when transaction completes too early
                    Dexie.currentTransaction.complete(function() {
                        ok(true, "Transaction committing too early...");
                        resolve();
                    });
                });
            }).then(function () {
                // Now when transaction has already committed, try to add a user with the current transaction:
                return db.users.add({ username: "arne" });
            }).then(function () {
                ok(false, "Should not be able to get a here transaction has become inactive");
            });
        }).then(function () {
            ok(false, "Should not be able to get a here transaction has become inactive");
        }).catch(function (err) {
            ok(true, "Got error because the transaction has already committed: " + err);
        }).finally(start);
    });

    asyncTest("sub-transactions", function () {
        var parentTrans;

        function addUser(user, pets) {
            return db.transaction('rw', db.users, db.pets, db.petsPerUser, function () {
                ok(parentTrans._reculock > 0, "Parent transaction is locked");
                db.users.add(user);
{'fraction_non_alphanumeric': 0.12720576952585547, 'fraction_numerical': 0.001611170784103115, 'mean_word_length': 1.9044117647058822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7118241', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3142, 'n_words': 1090}