Exemple #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void shouldHandleIndexDropConcurrentlyWithOperation(NodeOperation operation) throws Throwable
        private void ShouldHandleIndexDropConcurrentlyWithOperation(NodeOperation operation)
        {
            // given
            long[]          nodes           = CreateNodes();
            IndexDefinition indexDefinition = CreateIndex();

            // when
            Race race = new Race();

            race.AddContestant(() =>
            {
                using (Transaction tx = Db.beginTx())
                {
                    indexDefinition.Drop();
                    tx.Success();
                }
            }, 1);
            for (int i = 0; i < NODES; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long nodeId = nodes[i];
                long nodeId = nodes[i];
                race.AddContestant(throwing(() =>
                {
                    using (Transaction tx = Db.beginTx())
                    {
                        operation.Run(nodeId);
                        tx.Success();
                    }
                }));
            }

            // then
            race.Go();
        }
Exemple #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldDoProcessingInitializationInOrder() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldDoProcessingInitializationInOrder()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.test.Race race = new org.neo4j.test.Race();
            Race race = new Race();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicInteger coordination = new java.util.concurrent.atomic.AtomicInteger(-1);
            AtomicInteger coordination = new AtomicInteger(-1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicInteger expected = new java.util.concurrent.atomic.AtomicInteger();
            AtomicInteger expected = new AtomicInteger();
            const int     threads  = 30;

//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") final RecordCheckWorker<int>[] workers = new RecordCheckWorker[threads];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            RecordCheckWorker <int>[] workers = new RecordCheckWorker[threads];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RecordProcessor<int> processor = new RecordProcessor_Adapter<int>()
            RecordProcessor <int> processor = new RecordProcessor_AdapterAnonymousInnerClass(this, expected);

            for (int id = 0; id < threads; id++)
            {
                ArrayBlockingQueue <int> queue = new ArrayBlockingQueue <int>(10);
                race.AddContestant(workers[id] = new RecordCheckWorker <int>(id, coordination, queue, processor));
            }
            race.AddContestant(() =>
            {
                try
                {
                    long end = currentTimeMillis() + SECONDS.toMillis(100);
                    while (currentTimeMillis() < end && expected.get() < threads)
                    {
                        parkNanos(MILLISECONDS.toNanos(10));
                    }
                    assertEquals(threads, expected.get());
                }
                finally
                {
                    foreach (RecordCheckWorker <int> worker in workers)
                    {
                        worker.Done();
                    }
                }
            });

            // WHEN
            race.Go();
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldKeepHighestDuringConcurrentOfferings() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldKeepHighestDuringConcurrentOfferings()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final HighestTransactionId highest = new HighestTransactionId(-1, -1, -1);
            HighestTransactionId highest = new HighestTransactionId(-1, -1, -1);
            Race race     = new Race();
            int  updaters = max(2, Runtime.availableProcessors());
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicInteger accepted = new java.util.concurrent.atomic.AtomicInteger();
            AtomicInteger accepted = new AtomicInteger();

            for (int i = 0; i < updaters; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long id = i + 1;
                long id = i + 1;
                race.AddContestant(() =>
                {
                    if (highest.Offer(id, id, id))
                    {
                        accepted.incrementAndGet();
                    }
                });
            }

            // WHEN
            race.Go();

            // THEN
            assertTrue(accepted.get() > 0);
            assertEquals(updaters, highest.Get().transactionId());
        }
Exemple #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord()
        {
            Race race = new Race();

            long[]     latestNodeId = new long[1];
            AtomicLong writes       = new AtomicLong();
            AtomicLong reads        = new AtomicLong();
            long       endTime      = currentTimeMillis() + SECONDS.toMillis(2);

            race.WithEndCondition(() => (writes.get() > 100 && reads.get() > 10_000) || currentTimeMillis() > endTime);
            race.AddContestant(() =>
            {
                using (Transaction tx = Db.beginTx())
                {
                    Node node       = Db.createNode();
                    latestNodeId[0] = node.Id;
                    node.setProperty("largeProperty", LONG_STRING_VALUE);
                    tx.success();
                }
                writes.incrementAndGet();
            });
            race.AddContestant(() =>
            {
                try
                {
                    using (Transaction tx = Db.GraphDatabaseAPI.beginTx())
                    {
                        Node node = Db.GraphDatabaseAPI.getNodeById(latestNodeId[0]);

                        foreach (string propertyKey in node.PropertyKeys)
                        {
                            node.getProperty(propertyKey);
                        }
                        tx.success();
                    }
                }
                catch (NotFoundException e)
                {
                    if (Exceptions.contains(e, typeof(InvalidRecordException)))
                    {
                        throw e;
                    }
                }
                reads.incrementAndGet();
            });
            race.Go();
        }
Exemple #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWriteOutTheDynamicChainBeforeUpdatingThePropertyRecord() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWriteOutTheDynamicChainBeforeUpdatingThePropertyRecord()
        {
            Race race = new Race();

            long[]     latestNodeId = new long[1];
            AtomicLong writes       = new AtomicLong();
            AtomicLong reads        = new AtomicLong();
            long       endTime      = currentTimeMillis() + SECONDS.toMillis(2);

            race.WithEndCondition(() => (writes.get() > 100 && reads.get() > 10_000) || currentTimeMillis() > endTime);
            race.AddContestant(() =>
            {
                using (Transaction tx = Db.beginTx())
                {
                    Node node       = Db.createNode();
                    latestNodeId[0] = node.Id;
                    node.setProperty("largeProperty", LONG_STRING_VALUE);
                    tx.success();
                }
                writes.incrementAndGet();
            });
            race.AddContestant(() =>
            {
                try
                {
                    using (Transaction tx = Db.GraphDatabaseAPI.beginTx())
                    {
                        Node node = Db.GraphDatabaseAPI.getNodeById(latestNodeId[0]);
                        foreach (string propertyKey in node.PropertyKeys)
                        {
                            node.getProperty(propertyKey);
                        }
                        tx.success();
                    }
                }
                catch (NotFoundException)
                {
                    // This will catch nodes not found (expected) and also PropertyRecords not found (shouldn't happen
                    // but handled in shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord)
                }
                reads.incrementAndGet();
            });
            race.Go();
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldRegisterPeakMemoryUsage() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldRegisterPeakMemoryUsage()
        {
            // given
            ThreadSafePeakMemoryAllocationTracker tracker = new ThreadSafePeakMemoryAllocationTracker(GlobalMemoryTracker.Instance);
            int threads = 200;

            long[]            allocations = new long[threads];
            ThreadLocalRandom random      = ThreadLocalRandom.current();
            long sum = 0;

            for (int i = 0; i < allocations.Length; i++)
            {
                allocations[i] = random.Next(1, 10_000);
                sum           += allocations[i];
            }

            // when
            Race race = new Race();

            for (int i = 0; i < threads; i++)
            {
                int id = i;
                race.AddContestant(() => tracker.allocated(allocations[id]));
            }
            race.Go();
            long peakAfterAllocation = tracker.PeakMemoryUsage();

            LongStream.of(allocations).forEach(tracker.deallocated);
            long peakAfterDeallocation = tracker.PeakMemoryUsage();

            LongStream.of(allocations).forEach(tracker.allocated);
            tracker.Allocated(10);                 // <-- 10 more than previous peak
            long peakAfterHigherReallocation = tracker.PeakMemoryUsage();

            LongStream.of(allocations).forEach(tracker.deallocated);
            tracker.Deallocated(10);
            long peakAfterFinalDeallocation = tracker.PeakMemoryUsage();

            // then
            assertEquals(sum, peakAfterAllocation);
            assertEquals(sum, peakAfterDeallocation);
            assertEquals(sum + 10, peakAfterHigherReallocation);
            assertEquals(sum + 10, peakAfterFinalDeallocation);
        }
Exemple #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHandleConcurrentGetOrCreate() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHandleConcurrentGetOrCreate()
        {
            // GIVEN
            Groups groups = new Groups();
            Race   race   = new Race();
            string name   = "MyGroup";

            for (int i = 0; i < Runtime.Runtime.availableProcessors(); i++)
            {
                race.AddContestant(() =>
                {
                    Group group = groups.GetOrCreate(name);
                    assertEquals(LOWEST_NONGLOBAL_ID, group.Id());
                });
            }

            // WHEN
            race.Go();

            // THEN
            Group otherGroup = groups.GetOrCreate("MyOtherGroup");

            assertEquals(LOWEST_NONGLOBAL_ID + 1, otherGroup.Id());
        }
Exemple #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWriteOutThePropertyRecordBeforeReferencingItFromARelationshipRecord() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWriteOutThePropertyRecordBeforeReferencingItFromARelationshipRecord()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long node1Id;
            long node1Id;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long node2Id;
            long node2Id;

            using (Transaction tx = Db.beginTx())
            {
                Node node1 = Db.createNode();
                node1Id = node1.Id;

                Node node2 = Db.createNode();
                node2Id = node2.Id;

                tx.Success();
            }

            Race race = new Race();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long[] latestRelationshipId = new long[1];
            long[]     latestRelationshipId = new long[1];
            AtomicLong writes  = new AtomicLong();
            AtomicLong reads   = new AtomicLong();
            long       endTime = currentTimeMillis() + SECONDS.toMillis(2);

            race.WithEndCondition(() => (writes.get() > 100 && reads.get() > 10_000) || currentTimeMillis() > endTime);
            race.AddContestant(() =>
            {
                using (Transaction tx = Db.beginTx())
                {
                    Node node1 = Db.GraphDatabaseAPI.getNodeById(node1Id);
                    Node node2 = Db.GraphDatabaseAPI.getNodeById(node2Id);

                    Relationship rel        = node1.createRelationshipTo(node2, _friend);
                    latestRelationshipId[0] = rel.Id;
                    rel.setProperty("largeProperty", LONG_STRING_VALUE);

                    tx.Success();
                }
                writes.incrementAndGet();
            });
            race.AddContestant(() =>
            {
                try
                {
                    using (Transaction tx = Db.GraphDatabaseAPI.beginTx())
                    {
                        Relationship rel = Db.GraphDatabaseAPI.getRelationshipById(latestRelationshipId[0]);

                        foreach (string propertyKey in rel.PropertyKeys)
                        {
                            rel.getProperty(propertyKey);
                        }
                        tx.Success();
                    }
                }
                catch (NotFoundException e)
                {
                    if (Exceptions.contains(e, typeof(InvalidRecordException)))
                    {
                        throw e;
                    }
                }
                reads.incrementAndGet();
            });
            race.Go();
        }
Exemple #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotDeadlockOrCrashFromInconsistency() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotDeadlockOrCrashFromInconsistency()
        {
            // GIVEN (A) -[R]-> (B)
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.Node a;
            Node a;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.Node b;
            Node b;

            using (Transaction tx = Db.beginTx())
            {
                (a = Db.createNode()).createRelationshipTo(b = Db.createNode(), MyRelTypes.TEST);
                tx.Success();
            }

            // WHEN
            Race race = new Race();

            // a bunch of deleters
            for (int i = 0; i < 30; i++)
            {
                race.AddContestant(() =>
                {
                    for (int j = 0; j < 10; j++)
                    {
                        using (Transaction tx = Db.beginTx())
                        {
                            Node node = Random.nextBoolean() ? a : b;
                            foreach (Relationship relationship in node.Relationships)
                            {
                                try
                                {
                                    relationship.delete();
                                }
                                catch (NotFoundException e)
                                {
                                    // This is OK and expected since there are multiple threads deleting
                                    assertTrue(e.Message.contains("already deleted"));
                                }
                            }
                            tx.Success();
                        }
                    }
                });
            }

            // a bunch of creators
            for (int i = 0; i < 30; i++)
            {
                race.AddContestant(() =>
                {
                    for (int j = 0; j < 10; j++)
                    {
                        using (Transaction tx = Db.beginTx())
                        {
                            bool order = Random.nextBoolean();
                            Node start = order ? a : b;
                            Node end   = order ? b : a;
                            start.createRelationshipTo(end, MyRelTypes.Test);
                            tx.Success();
                        }
                    }
                });
            }

            // THEN there should be no thread throwing exception, especially DeadlockDetectedException
            race.Go();
        }
Exemple #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReadConsistentPropertyValues() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReadConsistentPropertyValues()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Node[] nodes = new Node[10];
            Node[] nodes = new Node[10];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String[] keys = new String[] {"1", "2", "3"};
            string[] keys = new string[] { "1", "2", "3" };
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String[] values = new String[] { longString('a'), longString('b'), longString('c')};
            string[] values = new string[] { LongString('a'), LongString('b'), LongString('c') };
            using (Transaction tx = Db.beginTx())
            {
                for (int i = 0; i < nodes.Length; i++)
                {
                    nodes[i] = Db.createNode();
                    foreach (string key in keys)
                    {
                        nodes[i].SetProperty(key, values[0]);
                    }
                }
                tx.Success();
            }

            int updaters = 10;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicLong updatersDone = new java.util.concurrent.atomic.AtomicLong(updaters);
            AtomicLong updatersDone = new AtomicLong(updaters);
            Race       race         = new Race();

            for (int i = 0; i < updaters; i++)
            {
                // Changers
                race.AddContestant(() =>
                {
                    try
                    {
                        ThreadLocalRandom random = ThreadLocalRandom.current();
                        for (int j = 0; j < 100; j++)
                        {
                            Node node  = nodes[random.Next(nodes.Length)];
                            string key = keys[random.Next(keys.Length)];
                            using (Transaction tx = Db.beginTx())
                            {
                                node.RemoveProperty(key);
                                tx.Success();
                            }
                            using (Transaction tx = Db.beginTx())
                            {
                                node.SetProperty(key, values[random.Next(values.Length)]);
                                tx.Success();
                            }
                        }
                    }
                    finally
                    {
                        updatersDone.decrementAndGet();
                    }
                });
            }
            for (int i = 0; i < 100; i++)
            {
                // Readers
                race.AddContestant(() =>
                {
                    ThreadLocalRandom random = ThreadLocalRandom.current();
                    while (updatersDone.get() > 0)
                    {
                        using (Transaction tx = Db.beginTx())
                        {
                            string value = ( string )nodes[random.Next(nodes.Length)].getProperty(keys[random.Next(keys.Length)], null);
                            assertTrue(value, string.ReferenceEquals(value, null) || ArrayUtil.contains(values, value));
                            tx.Success();
                        }
                    }
                });
            }

            // WHEN
            race.Go();
        }