protected override void Execute() { var start = Environment.TickCount; var random = new Random(TesterId); do { // for simulating a broken lock to verify that this test does what is intended, use x as conditional on _lock calls // var x = _testerId != random.Next(0, 1000); _lock.Lock(); Interlocked.Increment(ref _counter); try { Thread.Sleep(random.Next(1, 20)); // add some variability to iteration length so that threads aren't just running in lock step Assert.AreEqual(1, _counter, @"Tester {0} entered lock while another tester has lock", TesterId); FlagFailed(_counter != 1); } finally { Interlocked.Decrement(ref _counter); _lock.Unlock(); } } while (Environment.TickCount - start < TestLengthMs); }
internal DataAccessContext(string mutexName, string databaseFilename) { if (!string.IsNullOrEmpty(mutexName)) { _mutex = ExclusiveLock.CreateFileSystemLock(SqlCeDatabaseHelper <DicomStoreDataContext> .GetDatabaseFilePath(mutexName + databaseFilename)); _mutex.Lock(); } try { // initialize a connection and transaction _databaseFilename = databaseFilename; _connection = CreateConnection(); _transaction = _connection.BeginTransaction(IsolationLevel.ReadCommitted); _context = new DicomStoreDataContext(_connection); //_context.Log = Console.Out; } catch { _mutex.Unlock(); _mutex.Dispose(); _mutex = null; throw; } lock (_syncLock) { if (_staticConnection == null) { // This is done for performance reasons. It forces a connection to remain open while the // the app domain is running, so that the database is kept in memory. try { _staticConnection = CreateConnection(); } catch (Exception ex) { Platform.Log(LogLevel.Debug, ex, "Failed to initialize static connection to data store database"); } } } }