Skip to content

Commit

Permalink
add unit test for racy condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
yathit committed Mar 3, 2016
1 parent 86261bc commit f8dca67
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/crud/race_2_idb_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Test for Race condition</title>
</head>
<body>

<script src="../../../closure-library/closure/goog/base.js"></script>
<script src="../../../closure-library/closure/goog/deps.js"></script>
<script type="text/javascript" src="../../../ydn-base/src/deps.js"></script>
<script type="text/javascript" src="../../src/deps.js"></script>
<script type="text/javascript" >
goog.require('ydn.debug');
goog.require('goog.userAgent');
</script>
<script type="text/javascript" >
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.jsunit');
options = {mechanisms: ['indexeddb']};
if (goog.userAgent.MOBILE) {
var div = document.createElement('div');
console.log('running on mobile');
document.body.appendChild(div);
ydn.debug.log('ydn.db', 'finest', div);
}
goog.require('ydn.db.crud.Storage');
</script>
<script type="text/javascript" src="../../src/ydn/db/tr/inject.js"></script>
<script type="text/javascript" src="../../src/ydn/db/crud/inject.js"></script>
<script type="text/javascript" src="race_2_test.js"></script>

</body>
</html>
58 changes: 58 additions & 0 deletions test/crud/race_2_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

goog.require('goog.debug.Console');
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.jsunit');
goog.require('ydn.async');
goog.require('ydn.db.crud.Storage');
goog.require('ydn.debug');

var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
var to_del = [];


var setUp = function() {

ydn.json.POLY_FILL = true;
// ydn.debug.log('ydn.db', 'finest');
// ydn.db.crud.req.WebSql.DEBUG = true;
// ydn.debug.log('ydn.db', 'finest');
// ydn.db.tr.Serial.DEBUG = true;
//ydn.db.crud.req.IndexedDb.DEBUG = true;
// ydn.db.con.IndexedDb.DEBUG = true;

};

var tearDown = function() {
var name;
while (name = to_del.pop()) {
ydn.db.deleteDatabase(name, options.mechanisms[0]);
};
};


function test_racy() {
var db_name = 'test_racy_1';
var schema = {
stores: [{
name: 'store1',
keyPath: 'id'
}]
};
var data = [{id: 1}];
var db1 = new ydn.db.crud.Storage(db_name, schema, options);
asyncTestCase.waitForAsync('put in db1');
db1.put('store1', data).addCallback(function() {
var db2 = new ydn.db.crud.Storage(db_name, schema, options);
asyncTestCase.continueTesting();
asyncTestCase.waitForAsync('values');
db1.close();
db2.values('store1').addCallback(function(arr) {
assertArrayEquals(data, arr);
asyncTestCase.continueTesting();
ydn.db.deleteDatabase(db_name, db2.getType());
db2.close();
});
});
to_del.push(to_del);
}

0 comments on commit f8dca67

Please sign in to comment.