Search in sources :

Example 1 with IndexValue

use of com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue in project nigori by ucam-cl-dtg.

the class ConcurrencyTest method singleUserConcurrency.

/**
 * Test that one user can do lots of things at the same time.
 *
 * @throws NigoriCryptographyException
 * @throws IOException
 * @throws UnauthorisedException
 */
@Test
public void singleUserConcurrency() throws NigoriCryptographyException, IOException, UnauthorisedException {
    failed = false;
    Thread[] threads = new Thread[THREADS];
    final NigoriDatastore nigori = getStore();
    assertTrue("Not registered", nigori.register());
    try {
        final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
        for (int j = 0; j < THREADS; ++j) {
            threads[j] = new Thread() {

                @Override
                public void run() {
                    boolean succeeded = false;
                    try {
                        Random r = new Random();
                        for (int i = 0; i < AcceptanceTests.REPEAT * REPEAT_FACTOR; ++i) {
                            // check we can do this more than once
                            for (IndexValue iv : SetGetDeleteTest.testCases) {
                                // once round for each
                                final Index index = new Index(new byte[16]);
                                // need to generate some different indices
                                r.nextBytes(index.getBytes());
                                final byte[] value = iv.revvalue.getValue();
                                final Revision revision = iv.revvalue.getRevision();
                                assertTrue("Not put" + i, nigori.put(index, revision, value));
                                try {
                                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                                } finally {
                                    assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                                }
                                assertNull("Not deleted" + i, nigori.get(index));
                            }
                        }
                        succeeded = true;
                    } catch (Throwable e) {
                        exceptionList.add(e);
                    } finally {
                        if (!succeeded && !failed) {
                            failed = true;
                        }
                    }
                }
            };
        }
        startThenJoinThreads(threads);
        ifFailedPrintFailures(failed, exceptionList);
    } finally {
        assertTrue(nigori.unregister());
    }
}
Also used : Random(java.util.Random) Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) IndexValue(com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue) Test(org.junit.Test)

Aggregations

NigoriDatastore (com.google.nigori.client.NigoriDatastore)1 IndexValue (com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue)1 Index (com.google.nigori.common.Index)1 Revision (com.google.nigori.common.Revision)1 Random (java.util.Random)1 Test (org.junit.Test)1