//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldUpgradeAndDowngradeSameSharedLock() public virtual void ShouldUpgradeAndDowngradeSameSharedLock() { // when ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientB.acquireShared(LockTracer.NONE, NODE, 1L); LockIdentityExplorer sharedLockExplorer = new LockIdentityExplorer(NODE, 1L); Locks.accept(sharedLockExplorer); // then xclusive should wait for shared from other client to be released Future <object> exclusiveLockFuture = AcquireExclusive(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // and when ClientA.releaseShared(NODE, 1L); // exclusive lock should be received AssertNotWaiting(ClientB, exclusiveLockFuture); // and when releasing exclusive ClientB.releaseExclusive(NODE, 1L); // we still should have same read lock LockIdentityExplorer releasedLockExplorer = new LockIdentityExplorer(NODE, 1L); Locks.accept(releasedLockExplorer); // we still hold same lock as before assertEquals(sharedLockExplorer.LockIdentityHashCode, releasedLockExplorer.LockIdentityHashCode); }
static void ObserverPattern() { Console.WriteLine("-----Observer Pattern-----"); Server server = new Server(); ClientA clientA = new ClientA(); ClientB clientB = new ClientB(); ClientC clientC = new ClientC(); server.AddClients(clientA); server.AddClients(clientB); server.NotifyClient("Good Morning!"); server.AddClients(clientC); server.NotifyClient("Good Afternoon!"); server.RemoveClient(clientA); server.NotifyClient("Good Night!"); Console.ReadLine(); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldAcquireMultipleExclusiveLocks() public virtual void ShouldAcquireMultipleExclusiveLocks() { ClientA.acquireExclusive(LockTracer.NONE, NODE, 10, 100, 1000); assertFalse(ClientB.trySharedLock(NODE, 10)); assertFalse(ClientB.trySharedLock(NODE, 100)); assertFalse(ClientB.trySharedLock(NODE, 1000)); assertEquals(3, LockCount()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldUpgradeExclusiveOnTry() public virtual void ShouldUpgradeExclusiveOnTry() { // Given I've grabbed a shared lock ClientA.acquireShared(LockTracer.NONE, NODE, 1L); // When assertTrue(ClientA.tryExclusiveLock(NODE, 1L)); // Then I should be able to release it ClientA.releaseExclusive(NODE, 1L); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTryUpgradeSharedToExclusive() public virtual void ShouldTryUpgradeSharedToExclusive() { // Given I've grabbed an exclusive lock assertTrue(ClientA.trySharedLock(NODE, 1L)); // Then I can upgrade it to exclusive assertTrue(ClientA.tryExclusiveLock(NODE, 1L)); // And other clients are denied it assertFalse(ClientB.trySharedLock(NODE, 1L)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTryExclusiveLock() public virtual void ShouldTryExclusiveLock() { // Given I've grabbed an exclusive lock assertTrue(ClientA.tryExclusiveLock(NODE, 1L)); // Then other clients can't have exclusive locks assertFalse(ClientB.tryExclusiveLock(NODE, 1L)); // Nor can they have share locks assertFalse(ClientB.trySharedLock(NODE, 1L)); }
private static void Main(string[] args) { var a = new ClientA("Adam", 68); var b = new ClientB("Bob", 1.75); var c = new ClientC("Cheryl", 54.4, 1.65); Console.WriteLine("{0} is {1:0.0} lbs.", a.Name, a.WeightPounds()); Console.WriteLine("{0} is {1:0.0} inches tall.", b.Name, b.HeightInches()); Console.WriteLine("{0} is {1:0.0} lbs and {2:0.0} inches.", c.Name, c.WeightPounds(), c.HeightInches()); Console.ReadLine(); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void releaseMultipleSharedLocks() public virtual void ReleaseMultipleSharedLocks() { ClientA.acquireShared(LockTracer.NONE, NODE, 10, 100, 1000); assertEquals(3, LockCount()); ClientA.releaseShared(NODE, 100, 1000); assertEquals(1, LockCount()); assertFalse(ClientB.tryExclusiveLock(NODE, 10)); assertTrue(ClientB.tryExclusiveLock(NODE, 100)); assertTrue(ClientB.tryExclusiveLock(NODE, 1000)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void releaseTryLocksOnClose() public virtual void ReleaseTryLocksOnClose() { assertTrue(ClientA.trySharedLock(ResourceTypes.Node, 1L)); assertTrue(ClientB.tryExclusiveLock(ResourceTypes.Node, 2L)); ClientA.close(); ClientB.close(); LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(0, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldCountNumberOfActiveLocks() public virtual void ShouldCountNumberOfActiveLocks() { // given ClientA.acquireShared(LockTracer.NONE, LABEL, 0); ClientA.acquireShared(LockTracer.NONE, RELATIONSHIP, 17); ClientA.acquireShared(LockTracer.NONE, NODE, 12); // when long count = ClientA.activeLockCount(); // then assertEquals(3, count); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldReleaseExclusiveLocksAcquiredInABatch() public virtual void ShouldReleaseExclusiveLocksAcquiredInABatch() { ClientA.acquireExclusive(LockTracer.NONE, NODE, 1, 10, 100); assertEquals(3, LockCount()); ClientA.releaseExclusive(NODE, 1); assertEquals(2, LockCount()); ClientA.releaseExclusive(NODE, 10); assertEquals(1, LockCount()); ClientA.releaseExclusive(NODE, 100); assertEquals(0, LockCount()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void sharedShouldWaitForExclusive() public virtual void SharedShouldWaitForExclusive() { // When ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); // Then shared locks should wait Future <object> clientBLock = AcquireShared(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // And when ClientA.releaseExclusive(NODE, 1L); // Then this should not block AssertNotWaiting(ClientB, clientBLock); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldListLocksHeldByTheCurrentClient() public virtual void ShouldListLocksHeldByTheCurrentClient() { // given ClientA.acquireExclusive(LockTracer.NONE, NODE, 1, 2, 3); ClientA.acquireShared(LockTracer.NONE, NODE, 3, 4, 5); // when //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: java.util.stream.Stream<? extends ActiveLock> locks = clientA.activeLocks(); Stream <ActiveLock> locks = ClientA.activeLocks(); // then assertEquals(new HashSet <>(asList(exclusiveLock(NODE, 1), exclusiveLock(NODE, 2), exclusiveLock(NODE, 3), sharedLock(NODE, 3), sharedLock(NODE, 4), sharedLock(NODE, 5))), locks.collect(toSet())); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void mustReleaseUnpreparedLocksOnStop() public virtual void MustReleaseUnpreparedLocksOnStop() { // Given ClientA.acquireShared(_tracer, NODE, 1L); ClientA.acquireExclusive(_tracer, NODE, 2L); // When ClientA.stop(); // The client was stopped before it could enter the prepare phase, so all of its locks are released LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(0, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void mustNotReleaseLocksAfterPrepareOnStop() public virtual void MustNotReleaseLocksAfterPrepareOnStop() { // Given ClientA.acquireShared(_tracer, NODE, 1L); ClientA.acquireExclusive(_tracer, NODE, 2L); ClientA.prepare(); // When ClientA.stop(); // The client entered the prepare phase, so it gets to keep its locks LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(2, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void prepareMustAllowAcquiringNewLocksAfterStop() public virtual void PrepareMustAllowAcquiringNewLocksAfterStop() { // Given ClientA.prepare(); ClientA.stop(); // When ClientA.acquireShared(_tracer, NODE, 1); ClientA.acquireExclusive(_tracer, NODE, 2); // Stopped essentially has no effect when it comes after the client has entered the prepare phase LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(2, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldTraceWaitTimeWhenTryingToAcquireExclusiveLockAndSharedIsHeld() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldTraceWaitTimeWhenTryingToAcquireExclusiveLockAndSharedIsHeld() { // given Tracer tracerA = new Tracer(); Tracer tracerB = new Tracer(); ClientA.acquireShared(tracerA, NODE, 17); // when Future <object> future = AcquireExclusive(ClientB, tracerB, NODE, 17).callAndAssertWaiting(); // then ClientA.releaseShared(NODE, 17); future.get(); tracerA.AssertCalls(0); tracerB.AssertCalls(1); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void releaseMultipleAlreadyAcquiredExclusiveLocks() public virtual void ReleaseMultipleAlreadyAcquiredExclusiveLocks() { ClientA.acquireExclusive(LockTracer.NONE, NODE, 10, 100, 1000); ClientA.acquireExclusive(LockTracer.NONE, NODE, 100, 1000, 10000); ClientA.releaseExclusive(NODE, 100, 1000); assertEquals(4, LockCount()); assertFalse(ClientB.trySharedLock(NODE, 10)); assertFalse(ClientB.trySharedLock(NODE, 100)); assertFalse(ClientB.trySharedLock(NODE, 1000)); assertFalse(ClientB.trySharedLock(NODE, 10000)); ClientA.releaseExclusive(NODE, 100, 1000); assertEquals(2, LockCount()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void mustReleaseReadLockWaitersOnStop() public virtual void MustReleaseReadLockWaitersOnStop() { // Given ClientA.acquireExclusive(_tracer, NODE, 1L); ClientB.acquireExclusive(_tracer, NODE, 2L); AcquireShared(ClientB, _tracer, NODE, 1L).callAndAssertWaiting(); // When ClientB.stop(); ClientA.stop(); // All locks clients should be stopped at this point, and all all locks should be released because none of the // clients entered the prepare phase LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(0, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void releaseExclusiveLocksAcquiredSeparately() public virtual void ReleaseExclusiveLocksAcquiredSeparately() { ClientA.acquireExclusive(LockTracer.NONE, NODE, 1); ClientA.acquireExclusive(LockTracer.NONE, NODE, 2); ClientA.acquireExclusive(LockTracer.NONE, NODE, 3); assertEquals(3, LockCount()); assertFalse(ClientB.trySharedLock(NODE, 1)); assertFalse(ClientB.trySharedLock(NODE, 2)); assertFalse(ClientB.trySharedLock(NODE, 3)); ClientA.releaseExclusive(NODE, 1, 2, 3); assertEquals(0, LockCount()); assertTrue(ClientB.trySharedLock(NODE, 1)); assertTrue(ClientB.trySharedLock(NODE, 2)); assertTrue(ClientB.trySharedLock(NODE, 3)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void exclusiveShouldWaitForShared() public virtual void ExclusiveShouldWaitForShared() { // When ClientA.acquireShared(LockTracer.NONE, NODE, 1L); // Then other shared locks are allowed ClientC.acquireShared(LockTracer.NONE, NODE, 1L); // But exclusive locks should wait Future <object> clientBLock = AcquireExclusive(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // And when ClientA.releaseShared(NODE, 1L); ClientC.releaseShared(NODE, 1L); // Then this should not block AssertNotWaiting(ClientB, clientBLock); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void releaseMultipleExclusiveLocksWhileHavingSomeSharedLocks() public virtual void ReleaseMultipleExclusiveLocksWhileHavingSomeSharedLocks() { ClientA.acquireShared(LockTracer.NONE, NODE, 100, 1000, 10000); ClientA.acquireExclusive(LockTracer.NONE, NODE, 10, 100, 1000); assertFalse(ClientB.trySharedLock(NODE, 10)); assertFalse(ClientB.trySharedLock(NODE, 100)); assertFalse(ClientB.trySharedLock(NODE, 1000)); assertFalse(ClientB.tryExclusiveLock(NODE, 10000)); assertEquals(4, LockCount()); ClientA.releaseExclusive(NODE, 100, 1000); assertFalse(ClientB.trySharedLock(NODE, 10)); assertFalse(ClientB.tryExclusiveLock(NODE, 100)); assertFalse(ClientB.tryExclusiveLock(NODE, 1000)); assertFalse(ClientB.tryExclusiveLock(NODE, 10000)); assertEquals(4, LockCount()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldRetainSharedLockWhenAcquiredAfterExclusiveLock() public virtual void ShouldRetainSharedLockWhenAcquiredAfterExclusiveLock() { // When ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); ClientA.acquireShared(LockTracer.NONE, NODE, 1L); // Then this should wait Future <object> clientBLock = AcquireExclusive(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // And when ClientA.releaseExclusive(NODE, 1L); // Then other thread should still wait AssertWaiting(ClientB, clientBLock); // But when ClientA.releaseShared(NODE, 1L); // Then AssertNotWaiting(ClientB, clientBLock); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldAcquireExclusiveIfClientIsOnlyOneHoldingShared() public virtual void ShouldAcquireExclusiveIfClientIsOnlyOneHoldingShared() { // When ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); // Then shared locks should wait Future <object> clientBLock = AcquireExclusive(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // And when ClientA.releaseExclusive(NODE, 1L); // Then other thread should still wait AssertWaiting(ClientB, clientBLock); // But when ClientA.releaseShared(NODE, 1L); // Then AssertNotWaiting(ClientB, clientBLock); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void exclusiveLocksShouldBeReentrantAndBlockOtherSharedLocks() public virtual void ExclusiveLocksShouldBeReentrantAndBlockOtherSharedLocks() { // When ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientA.tryExclusiveLock(NODE, 1L); // Then exclusive locks should wait Future <object> clientBLock = AcquireShared(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // And when ClientA.releaseExclusive(NODE, 1L); ClientA.releaseShared(NODE, 1L); // Then other thread should still wait AssertWaiting(ClientB, clientBLock); // But when ClientA.releaseExclusive(NODE, 1L); // Then AssertNotWaiting(ClientB, clientBLock); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void closeShouldWaitAllOperationToFinish() public virtual void CloseShouldWaitAllOperationToFinish() { // given ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientA.acquireShared(LockTracer.NONE, NODE, 3L); ClientB.acquireShared(LockTracer.NONE, NODE, 1L); AcquireShared(ClientC, LockTracer.NONE, NODE, 2L); AcquireExclusive(ClientB, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); AcquireExclusive(ClientC, LockTracer.NONE, NODE, 1L).callAndAssertWaiting(); // when ClientB.close(); ClientC.close(); ClientA.close(); // all locks should be closed at this point regardless of // reader/writer waiter in any threads // those should be gracefully finish and client should be closed LockCountVisitor lockCountVisitor = new LockCountVisitor(); Locks.accept(lockCountVisitor); assertEquals(0, lockCountVisitor.LockCount); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(expected = LockClientStoppedException.class) public void shouldNotBeAbleToAcquireSharedLockFromClosedClient() public virtual void ShouldNotBeAbleToAcquireSharedLockFromClosedClient() { ClientA.close(); ClientA.acquireShared(LockTracer.NONE, NODE, 1L); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(expected = LockClientStoppedException.class) public void shouldNotBeAbleToTryAcquireExclusiveLockFromClosedClient() public virtual void ShouldNotBeAbleToTryAcquireExclusiveLockFromClosedClient() { ClientA.close(); ClientA.tryExclusiveLock(NODE, 1L); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(expected = LockClientStoppedException.class) public void shouldNotBeAbleToTryAcquireSharedLockFromClosedClient() public virtual void ShouldNotBeAbleToTryAcquireSharedLockFromClosedClient() { ClientA.close(); ClientA.trySharedLock(NODE, 1L); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testSingleThread() public virtual void TestSingleThread() { try { ClientA.releaseExclusive(NODE, 1L); fail("Invalid release should throw exception"); } catch (Exception) { // good } try { ClientA.releaseShared(NODE, 1L); fail("Invalid release should throw exception"); } catch (Exception) { // good } ClientA.acquireShared(LockTracer.NONE, NODE, 1L); try { ClientA.releaseExclusive(NODE, 1L); fail("Invalid release should throw exception"); } catch (Exception) { // good } ClientA.releaseShared(NODE, 1L); ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); try { ClientA.releaseShared(NODE, 1L); fail("Invalid release should throw exception"); } catch (Exception) { // good } ClientA.releaseExclusive(NODE, 1L); ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); ClientA.releaseExclusive(NODE, 1L); ClientA.releaseShared(NODE, 1L); ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); ClientA.acquireShared(LockTracer.NONE, NODE, 1L); ClientA.releaseShared(NODE, 1L); ClientA.releaseExclusive(NODE, 1L); for (int i = 0; i < 10; i++) { if ((i % 2) == 0) { ClientA.acquireExclusive(LockTracer.NONE, NODE, 1L); } else { ClientA.acquireShared(LockTracer.NONE, NODE, 1L); } } for (int i = 9; i >= 0; i--) { if ((i % 2) == 0) { ClientA.releaseExclusive(NODE, 1L); } else { ClientA.releaseShared(NODE, 1L); } } }