Beispiel #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldTellWhenTransactionsFromSnapshotHaveBeenClosed() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldTellWhenTransactionsFromSnapshotHaveBeenClosed()
        {
            // GIVEN
            KernelTransactions         transactions = NewKernelTransactions();
            KernelTransaction          a            = GetKernelTransaction(transactions);
            KernelTransaction          b            = GetKernelTransaction(transactions);
            KernelTransaction          c            = GetKernelTransaction(transactions);
            KernelTransactionsSnapshot snapshot     = transactions.Get();

            assertFalse(snapshot.AllClosed());

            // WHEN a gets closed
            a.Close();
            assertFalse(snapshot.AllClosed());

            // WHEN c gets closed and (test knowing too much) that instance getting reused in another transaction "d".
            c.Close();
            KernelTransaction d = GetKernelTransaction(transactions);

            assertFalse(snapshot.AllClosed());

            // WHEN b finally gets closed
            b.Close();
            assertTrue(snapshot.AllClosed());
        }
Beispiel #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeAbleToSnapshotDuringHeavyLoad()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final KernelTransactions transactions = newKernelTransactions();
            KernelTransactions transactions = NewKernelTransactions();
            Race      race    = new Race();
            const int threads = 50;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean end = new java.util.concurrent.atomic.AtomicBoolean();
            AtomicBoolean end = new AtomicBoolean();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new java.util.concurrent.atomic.AtomicReferenceArray<>(threads);
            AtomicReferenceArray <KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray <KernelTransactionsSnapshot>(threads);

            // Representing "transaction" threads
            for (int i = 0; i < threads; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int threadIndex = i;
                int threadIndex = i;
                race.AddContestant(() =>
                {
                    ThreadLocalRandom random = ThreadLocalRandom.current();
                    while (!end.get())
                    {
                        try
                        {
                            using (KernelTransaction transaction = GetKernelTransaction(transactions))
                            {
                                KernelTransactionsSnapshot snapshot = null;
                                try
                                {
                                    parkNanos(MILLISECONDS.toNanos(random.Next(3)));
                                    if (snapshots.get(threadIndex) == null)
                                    {
                                        requireNonNull(transactions, "transactions is null");
                                        snapshot = requireNonNull(transactions.Get(), "transactions.get() returned null");
                                        snapshots.set(threadIndex, snapshot);
                                        parkNanos(MILLISECONDS.toNanos(random.Next(3)));
                                    }
                                }
                                catch (Exception e)
                                {
                                    StringBuilder sb = (new StringBuilder("Gotcha!\n")).Append("threadIndex=").Append(threadIndex).Append('\n').Append("transaction=").Append(transaction).Append('\n').Append("snapshots=").Append(snapshots).Append('\n').Append("snapshot=").Append(snapshot).Append('\n').Append("end=").Append(end);
                                    throw new Exception(sb.ToString(), e);
                                }
                            }
                        }
                        catch (TransactionFailureException e)
                        {
                            throw new Exception(e);
                        }
                    }
                });
            }

            // Just checks snapshots
            race.AddContestant(() =>
            {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                int snapshotsLeft        = 1_000;
                while (snapshotsLeft > 0)
                {
                    int threadIndex = random.Next(threads);
                    KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex);
                    if (snapshot != null && snapshot.AllClosed())
                    {
                        snapshotsLeft--;
                        snapshots.set(threadIndex, null);
                    }
                }

                // End condition of this test can be described as:
                //   when 1000 snapshots have been seen as closed.
                // setting this boolean to true will have all other threads end as well so that race.go() will end
                end.set(true);
            });

            // WHEN
            race.Go();
        }