Node.js脚本中的竞争条件

时间:2019-05-16 17:43:15

标签: javascript node.js mocha

我正在为SQLite使用以下数据库连接器:

states.js

const sqlite3 = require('sqlite3').verbose();
const util = require('util');

async function getDB() {
    return new Promise(function(resolve, reject) {
        let db = new sqlite3.Database('./project.db', (err) => {
          if (err) {
            console.error(err.message);
                reject(err)
          } else {
            console.log('Connected to the project database.');
                resolve(db)
        }
        });
        return db
    });
}

exports.getDB = getDB

try {
        // run these statements once to set up the db
        // let db = getDB();
        // db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`);
        // db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
} catch(err) {
    console.log(err)
}

exports.get = async function(service) {
     function getResults(service) {
     return new Promise(async function (resolve, reject) {
                const db = await getDB();
                let sql = `SELECT Id id,
                               Service service,
                                                Date date
                        FROM services
                        WHERE service  = ?`;
                 db.get(sql, [service], (err, row) => {
                    if (err) {
                     console.error(err.message);
                     reject(err)
                 } else {
                     if (row) {
                         let this_row = {'id': row.id, 'service': row.service, 'date': row.date};
                         this_row ? console.log(row.id, row.service, row.date) : console.log(`No service found with the name ${service}`);
                         resolve(this_row)
                     } else {
                         resolve(null)
                     }
                 }
                })
     });
  }
    let row = await getResults(service)
    return row
}


exports.set = async function(service, state) {
    function setResults(service) {
        return new Promise(async function (resolve, reject) {
             const db = await getDB();
             let sql = `UPDATE services
                                set id = ?,
                                            service = ?,
                                            date = ?
                                 WHERE service  = ?`;
                try {
                    db.run(sql, [state.id, service, state.date, service]);
                    resolve();
                } catch(err) {
                    reject(err);
                }
    });
  }
    await setResults(service)
}


exports.clear = async function(service) {
    function deleteResults(service) {
        return new Promise(async function (resolve, reject) {
             const db = await getDB();
             let sql = `DELETE from services
                                  WHERE service  = ?`;
                db.run(sql, [service]);
        });
    }
    await deleteResults(service)
}

我的测试文件如下

const mocha = require('mocha');

const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();

const state = require('../state');

let deletion_sql = `DELETE from services WHERE service  = ?`;


it("get() should return the expected row", async function() {
    let db = await state.getDB()
    await db.run(deletion_sql, 'blah')
    await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
    let result = await state.get('blah')
    console.log("get test result is")
    console.log(result)
    assert.deepEqual(result, { 'id': 1, 'service': 'blah', 'date': '01-23-1987' })
});


it("set() should set to new values as expected", async function() {
    let db = await state.getDB()
    await db.run(deletion_sql, 'blah')
    await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
    await state.set('blah', {'id': 5, 'date': '05-22-2222'})
    let result = await state.get('blah')
    console.log("set test result is")
    console.log(result)
    assert.deepEqual(result, { 'id': 5, 'service': 'blah', 'date': '05-22-2222' })
});

it("clear() should delete row from db", async function() {
  state.clear('blah').then(async function() {
    let result = await state.get('blah')
    assert.equal(result, null)
  })
})

第一次和第三次测试通过,但是第二次测试失败的时间大约是一半,并且工作了一半的时间,如果失败,它将第二次测试显示为旧对象{ 'id': 1, 'service': 'blah', 'date': '01-23-1987' }。这告诉我,在测试运行set时,更新(.assert.deepEqual)尚未完成。

我在理解每个数据库操作的测试期间调用await时为什么会出现争用情况时遇到问题。我以为每个人都等待另一个人完成,到我从数据库中读取数据时就没有问题了。

0 个答案:

没有答案