Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tstl

Package Overview
Dependencies
Maintainers
1
Versions
359
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tstl - npm Package Compare versions

Comparing version 1.6.0-dev.20171026 to 1.6.0-dev.20171027

src/test/src/threads/condition_variables.ts

354

lib/test.js

@@ -515,9 +515,2 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

(function (test) {
function test_exception() {
}
test.test_exception = test_exception;
})(test || (test = {}));
/// <reference path="../API.ts" />
var test;
(function (test) {
function bind() {

@@ -596,9 +589,2 @@ var list = new std.List();

(function (test) {
function test_iterators() {
}
test.test_iterators = test_iterators;
})(test || (test = {}));
/// <reference path="../API.ts" />
var test;
(function (test) {
var Atomic = /** @class */ (function () {

@@ -665,5 +651,345 @@ function Atomic(value) {

})(test || (test = {}));
/// <refernce path="../API.ts" />
var test;
(function (test) {
var SLEEP_TIME = 100;
var WAIT_COUNT = 10;
function test_condition_variables() {
return __awaiter(this, void 0, void 0, function () {
var cv, wait_count, i, success_count, start_time, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
cv = new std.ConditionVariable();
wait_count = 0;
//----
// WAIT & NOTIFY
//----
// THERE'RE 10 WAITERS; HOLDERS
for (i = 0; i < WAIT_COUNT; ++i) {
cv.wait().then(function () {
--wait_count;
});
++wait_count;
}
// NOTIFY ONE
cv.notify_one();
return [4 /*yield*/, std.sleep_for(SLEEP_TIME)];
case 1:
_a.sent();
if (wait_count != WAIT_COUNT - 1)
throw new std.DomainError("Error on ConditionVariable::notify_one.");
// NOTIFY ALL
cv.notify_all();
return [4 /*yield*/, std.sleep_for(SLEEP_TIME)];
case 2:
_a.sent();
if (wait_count != 0)
throw new std.DomainError("Error on ConditionVariable::notify_all.");
success_count = 0;
start_time = new Date().getTime();
// THERE'RE 10 WAITERS, HOLDERS, WITH DIFFERENT TIMES
for (i = 0; i < WAIT_COUNT; ++i) {
cv.wait_for(i * SLEEP_TIME).then(function (ret) {
if (ret == true)
++success_count;
});
}
// NOTIFY ONE
cv.notify_one();
// NOTIFY ALL WHEN BE HALT TIME
return [4 /*yield*/, std.sleep_for(5 * SLEEP_TIME)];
case 3:
// NOTIFY ALL WHEN BE HALT TIME
_a.sent();
cv.notify_all();
// VALIDATE SUCCESS COUNT
return [4 /*yield*/, std.sleep_for(SLEEP_TIME)];
case 4:
// VALIDATE SUCCESS COUNT
_a.sent();
if (success_count < 3 || success_count > 7)
throw new std.DomainError("ConditionVariable::wait_for does not work in exact time.");
return [2 /*return*/];
}
});
});
}
test.test_condition_variables = test_condition_variables;
})(test || (test = {}));
/// <reference path="../API.ts" />
var test;
(function (test) {
var SLEEP_TIME = 50;
var READ_COUNT = 10;
function test_mutexes() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _Test_lock("Mutex", new std.Mutex())];
case 1:
_a.sent();
console.log(" mutex");
return [4 /*yield*/, _Test_try_lock("TimedMutex", new std.TimedMutex())];
case 2:
_a.sent();
console.log(" timed_mutex");
return [4 /*yield*/, _Test_lock_shared("SharedMutex", new std.SharedMutex())];
case 3:
_a.sent();
console.log(" shared_mutex");
return [4 /*yield*/, _Test_try_lock_shared("SharedTimedMutex", new std.SharedTimedMutex())];
case 4:
_a.sent();
console.log(" shared_timed_mutex");
return [2 /*return*/];
}
});
});
}
test.test_mutexes = test_mutexes;
/* ---------------------------------------------------------
WRITE LOCK
--------------------------------------------------------- */
function _Test_lock(name, mtx) {
return __awaiter(this, void 0, void 0, function () {
var start_time, elapsed_time;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
start_time = new Date().getTime();
// LOCK FOR A SECOND
mtx.lock();
std.sleep_for(SLEEP_TIME).then(function () {
mtx.unlock();
});
// TRY LOCK AGAIN
return [4 /*yield*/, mtx.lock()];
case 1:
// TRY LOCK AGAIN
_a.sent();
elapsed_time = new Date().getTime() - start_time;
mtx.unlock();
if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + " does not work.");
return [2 /*return*/];
}
});
});
}
function _Test_try_lock(name, mtx) {
return __awaiter(this, void 0, void 0, function () {
var start_time, ret, elapsed_time;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _Test_lock(name, mtx)];
case 1:
_a.sent();
start_time = new Date().getTime();
return [4 /*yield*/, mtx.try_lock_for(SLEEP_TIME)];
case 2:
ret = _a.sent();
if (ret == false)
throw new std.DomainError(name + "::try_lock_for does not return exact value.");
return [4 /*yield*/, mtx.try_lock_for(SLEEP_TIME)];
case 3:
// TRY LOCK AGAIN
ret = _a.sent();
elapsed_time = new Date().getTime() - start_time;
if (ret == true)
throw new std.DomainError(name + "::try_lock_for does not return exact value.");
else if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + " does not work in exact time.");
mtx.unlock();
return [2 /*return*/];
}
});
});
}
/* ---------------------------------------------------------
READ LOCK
--------------------------------------------------------- */
function _Test_lock_shared(name, mtx) {
return __awaiter(this, void 0, void 0, function () {
var read_count, i, start_time, elapsed_time, i, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// TEST WRITING LOCK & UNLOCK
return [4 /*yield*/, _Test_lock(name, mtx)];
case 1:
// TEST WRITING LOCK & UNLOCK
_a.sent();
read_count = 0;
for (i = 0; i < READ_COUNT; ++i) {
mtx.lock_shared();
++read_count;
}
if (read_count != READ_COUNT)
throw new std.DomainError(name + "::lock_shared does not support simultaneous lock.");
start_time = new Date().getTime();
std.sleep_for(SLEEP_TIME).then(function () {
// SLEEP FOR A SECOND AND UNLOCK ALL READINGS
for (var i = 0; i < READ_COUNT; ++i)
mtx.unlock_shared();
});
// DO WRITE LOCK; MUST BE BLOCKED
return [4 /*yield*/, mtx.lock()];
case 2:
// DO WRITE LOCK; MUST BE BLOCKED
_a.sent();
elapsed_time = new Date().getTime() - start_time;
if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + " does not block writing until reading.");
//----
// WRITE FIRST, READ LATER
//----
start_time = new Date().getTime();
std.sleep_for(SLEEP_TIME).then(function () {
// SLEEP FOR A SECOND AND UNLOCK WRITINGS
mtx.unlock();
});
i = 0;
_a.label = 3;
case 3:
if (!(i < READ_COUNT)) return [3 /*break*/, 6];
return [4 /*yield*/, mtx.lock_shared()];
case 4:
_a.sent();
_a.label = 5;
case 5:
++i;
return [3 /*break*/, 3];
case 6:
// VALIDATE ELAPSED TIME
elapsed_time = new Date().getTime() - start_time;
if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + " does not block reading until writing.");
// RELEASE READING LOCK FOR THE NEXT STEP
for (i = 0; i < READ_COUNT; ++i)
mtx.unlock_shared();
return [2 /*return*/];
}
});
});
}
function _Test_try_lock_shared(name, mtx) {
return __awaiter(this, void 0, void 0, function () {
var start_time, elapsed_time, flag, i, i, i, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// TEST WRITING LOCK & UNLOCK
return [4 /*yield*/, _Test_try_lock(name, mtx)];
case 1:
// TEST WRITING LOCK & UNLOCK
_a.sent();
return [4 /*yield*/, _Test_lock_shared(name, mtx)];
case 2:
_a.sent();
//----
// READ SIMULTANEOUSLY
//----
start_time = new Date().getTime();
i = 0;
_a.label = 3;
case 3:
if (!(i < READ_COUNT)) return [3 /*break*/, 6];
return [4 /*yield*/, mtx.try_lock_shared_for(SLEEP_TIME)];
case 4:
flag = _a.sent();
if (flag == false)
throw new std.DomainError(name + "::try_lock_shared_for does not return exact value.");
_a.label = 5;
case 5:
++i;
return [3 /*break*/, 3];
case 6:
// VALIDATE ELAPSED TIME
elapsed_time = new Date().getTime() - start_time;
if (elapsed_time >= SLEEP_TIME)
throw new std.DomainError(name + "::try_lock_shared_for does not support simultaneous lock.");
//----
// WRITE LOCK
//----
// TRY WRITE LOCK ON READING
start_time = new Date().getTime();
return [4 /*yield*/, mtx.try_lock_for(SLEEP_TIME)];
case 7:
flag = _a.sent();
elapsed_time = new Date().getTime() - start_time;
if (flag == true)
throw new std.DomainError(name + "::try_lock_for does not return exact value on reading.");
else if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + "::try_lock_for does not block on reading.");
// TRY WRITE LOCK AFTER READING
std.sleep_for(SLEEP_TIME).then(function () {
for (var i = 0; i < READ_COUNT; ++i)
mtx.unlock_shared();
});
start_time = new Date().getTime();
return [4 /*yield*/, mtx.try_lock_for(SLEEP_TIME)];
case 8:
flag = _a.sent();
elapsed_time = new Date().getTime() - start_time;
if (flag == false)
throw new std.DomainError(name + "::try_lock_for does not return exact value on reading.");
else if (elapsed_time < SLEEP_TIME * .95)
throw new std.DomainError(name + "::try_lock_for does not work in exact time.");
//----
// READ LOCK
//----
// READ LOCK ON WRITING
start_time = new Date().getTime();
i = 0;
_a.label = 9;
case 9:
if (!(i < READ_COUNT)) return [3 /*break*/, 12];
return [4 /*yield*/, mtx.try_lock_shared_for(SLEEP_TIME)];
case 10:
flag = _a.sent();
if (flag == true)
throw new std.DomainError(name + "::try_lock_shared_for does not return exact value on writing.");
_a.label = 11;
case 11:
++i;
return [3 /*break*/, 9];
case 12:
elapsed_time = new Date().getTime() - start_time;
if (elapsed_time < SLEEP_TIME * READ_COUNT * .95)
return [2 /*return*/, new std.DomainError(name + "::try_lock_shared_for does not work in exact time.")];
// READ LOCK AFTER WRITING
start_time = new Date().getTime();
std.sleep_for(SLEEP_TIME).then(function () {
mtx.unlock();
});
i = 0;
_a.label = 13;
case 13:
if (!(i < READ_COUNT)) return [3 /*break*/, 16];
return [4 /*yield*/, mtx.try_lock_shared_for(SLEEP_TIME)];
case 14:
flag = _a.sent();
if (flag == false)
throw new std.DomainError(name + "::try_lock_shared_for does not return exact value after writing.");
_a.label = 15;
case 15:
++i;
return [3 /*break*/, 13];
case 16:
elapsed_time = new Date().getTime() - start_time;
if (elapsed_time < SLEEP_TIME * .95 || elapsed_time >= SLEEP_TIME * 5.0)
throw new std.DomainError("::try_lock_shared_for does not work in exact time.");
// RELEASE READING LOCK FOR THE NEXT STEP
for (i = 0; i < READ_COUNT; ++i)
mtx.unlock_shared();
return [2 /*return*/];
}
});
});
}
})(test || (test = {}));
/// <reference path="../API.ts" />
var test;
(function (test) {
function test_sleeps() {

@@ -670,0 +996,0 @@ return __awaiter(this, void 0, void 0, function () {

2

package.json

@@ -10,3 +10,3 @@ {

"version": "1.6.0-dev.20171026",
"version": "1.6.0-dev.20171027",
"main": "./lib/tstl.js",

@@ -13,0 +13,0 @@ "typings": "./lib/tstl.d.ts",

@@ -44,3 +44,3 @@ /// <reference path="../API.ts" />

{
if (this.read_lock_count_ == 0 && this.write_lock_count_++ == 0)
if (this.write_lock_count_++ == 0 && this.read_lock_count_ == 0)
resolve();

@@ -47,0 +47,0 @@ else

@@ -44,3 +44,3 @@ /// <reference path="../API.ts" />

{
if (this.read_lock_count_ == 0 && this.write_lock_count_++ == 0)
if (this.write_lock_count_++ == 0 && this.read_lock_count_ == 0)
resolve();

@@ -69,3 +69,3 @@ else

{
if (this.read_lock_count_ == 0 && this.write_lock_count_++ == 0)
if (this.write_lock_count_++ == 0 && this.read_lock_count_ == 0)
resolve(true);

@@ -72,0 +72,0 @@ else

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc