File: CursorTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (975 lines) | stat: -rw-r--r-- 27,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: CursorTest.java,v 1.82.2.2 2010/01/04 15:30:42 cwl Exp $
 */

package com.sleepycat.je;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import junit.framework.TestCase;

import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.DatabaseImpl;
import com.sleepycat.je.junit.JUnitThread;
import com.sleepycat.je.util.TestUtils;

public class CursorTest extends TestCase {
    private static final boolean DEBUG = false;
    private static final int NUM_RECS = 257;

    /*
     * Use a ridiculous value because we've seen extreme slowness on ocicat
     * where dbperf is often running.
     */
    private static final long LOCK_TIMEOUT = 50000000L;

    private static final String DUPKEY = "DUPKEY";

    private Environment env;
    private Database db;
    private PhantomTestConfiguration config;

    private File envHome;

    private volatile int sequence;

    public CursorTest() {
        envHome = new File(System.getProperty(TestUtils.DEST_DIR));
    }

    public void setUp()
	throws IOException {

        TestUtils.removeLogFiles("Setup", envHome, false);
    }

    public void tearDown()
	throws IOException {

        if (env != null) {
            try {
                env.close();
            } catch (Throwable e) {
                System.out.println("tearDown: " + e);
            }
        }
	db = null;
	env = null;

        TestUtils.removeLogFiles("TearDown", envHome, false);
    }

    public void testGetConfig()
	throws DatabaseException {

        EnvironmentConfig envConfig = TestUtils.initEnvConfig();
        envConfig.setTransactional(true);
        envConfig.setAllowCreate(true);
        envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
        env = new Environment(envHome, envConfig);
        Transaction txn = env.beginTransaction(null, null);
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setSortedDuplicates(true);
        dbConfig.setAllowCreate(true);
        db = env.openDatabase(txn, "testDB", dbConfig);
	txn.commit();
	Cursor cursor = null;
	Transaction txn1 =
	    env.beginTransaction(null, TransactionConfig.DEFAULT);
	try {
	    cursor = db.openCursor(txn1, CursorConfig.DEFAULT);
	    CursorConfig config = cursor.getConfig();
	    if (config == CursorConfig.DEFAULT) {
		fail("didn't clone");
	    }
	} catch (DatabaseException DBE) {
	    DBE.printStackTrace();
	    fail("caught DatabaseException " + DBE);
	} finally {
	    if (cursor != null) {
		cursor.close();
	    }
	    txn1.abort();
	    db.close();
	    env.close();
            env = null;
	}
    }

    /**
     * Put some data in a database, take it out. Yank the file size down so we
     * have many files.
     */
    public void testBasic()
	throws Throwable {

	try {
	    insertMultiDb(1);
	} catch (Throwable t) {
	    t.printStackTrace();
	    throw t;
	}
    }

    public void testMulti()
	throws Throwable {

	try {
	    insertMultiDb(4);
	} catch (Throwable t) {
	    t.printStackTrace();
	    throw t;
	}
    }

    /**
     * Specifies a test configuration.  This is just a struct for holding
     * parameters to be passed down to threads in inner classes.
     */
    class PhantomTestConfiguration {
	String testName;
	String thread1EntryToLock;
	String thread1OpArg;
	String thread2Start;
	String expectedResult;
	boolean doInsert;
	boolean doGetNext;
	boolean doCommit;

	PhantomTestConfiguration(String testName,
				 String thread1EntryToLock,
				 String thread1OpArg,
				 String thread2Start,
				 String expectedResult,
				 boolean doInsert,
				 boolean doGetNext,
				 boolean doCommit) {
	    this.testName = testName;
	    this.thread1EntryToLock = thread1EntryToLock;
	    this.thread1OpArg = thread1OpArg;
	    this.thread2Start = thread2Start;
	    this.expectedResult = expectedResult;
	    this.doInsert = doInsert;
	    this.doGetNext = doGetNext;
	    this.doCommit = doCommit;
	}
    }

    /**
     * This series of tests sets up a simple 2 BIN tree with a specific set of
     * elements (see setupDatabaseAndEnv()).  It creates two threads.
     *
     * Thread 1 positions a cursor on an element on the edge of a BIN (either
     * the last element on the left BIN or the first element on the right BIN).
     * This locks that element.  It throws control to thread 2.
     *
     * Thread 2 positions a cursor on the adjacent element on the other BIN
     * (either the first element on the right BIN or the last element on the
     * left BIN, resp.)  It throws control to thread 1.  After it signals
     * thread 1 to continue, thread 2 does either a getNext or getPrev.  This
     * should block because thread 1 has the next/prev element locked.
     *
     * Thread 1 then waits a short time (250ms) so that thread 2 can execute
     * the getNext/getPrev.  Thread 1 then inserts or deletes the "phantom
     * element" right in between the cursors that were set up in the previous
     * two steps, sleeps a second, and either commits or aborts.
     *
     * Thread 2 will then return from the getNext/getPrev.  The returned key
     * from the getNext/getPrev is then verified.
     *
     * The Serializable isolation level is not used for either thread so as to
     * allow phantoms; otherwise, this test would deadlock.
     *
     * These parameters are all configured through a PhantomTestConfiguration
     * instance passed to phantomWorker which has the template for the steps
     * described above.
     */

    /**
     * Phantom test inserting and committing a phantom while doing a getNext.
     */
    public void testPhantomInsertGetNextCommit()
	throws Throwable {

        try {
            phantomWorker
                (new PhantomTestConfiguration
                 ("testPhantomInsertGetNextCommit",
                  "F", "D", "C", "D",
                  true, true, true));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * Phantom test inserting and aborting a phantom while doing a getNext.
     */
    public void testPhantomInsertGetNextAbort()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomInsertGetNextAbort",
	      "F", "D", "C", "F",
	      true, true, false));
    }

    /**
     * Phantom test inserting and committing a phantom while doing a getPrev.
     */
    public void testPhantomInsertGetPrevCommit()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomInsertGetPrevCommit",
	      "C", "F", "G", "F",
	      true, false, true));
    }

    /**
     * Phantom test inserting and aborting a phantom while doing a getPrev.
     */
    public void testPhantomInsertGetPrevAbort()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomInsertGetPrevAbort",
	      "C", "F", "G", "C",
	      true, false, false));
    }

    /**
     * Phantom test deleting and committing an edge element while doing a
     * getNext.
     */
    public void testPhantomDeleteGetNextCommit()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDeleteGetNextCommit",
	      "F", "F", "C", "G",
	      false, true, true));
    }

    /**
     * Phantom test deleting and aborting an edge element while doing a
     * getNext.
     */
    public void testPhantomDeleteGetNextAbort()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDeleteGetNextAbort",
	      "F", "F", "C", "F",
	      false, true, false));
    }

    /**
     * Phantom test deleting and committing an edge element while doing a
     * getPrev.
     */
    public void testPhantomDeleteGetPrevCommit()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDeleteGetPrevCommit",
	      "F", "F", "G", "C",
	      false, false, true));
    }

    /**
     * Phantom test deleting and aborting an edge element while doing a
     * getPrev.
     */
    public void testPhantomDeleteGetPrevAbort()
	throws Throwable {

	phantomWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDeleteGetPrevAbort",
	      "F", "F", "G", "F",
	      false, false, false));
    }

    /**
     * Phantom Dup test inserting and committing a phantom while doing a
     * getNext.
     */
    public void testPhantomDupInsertGetNextCommit()
	throws Throwable {

        try {
            phantomDupWorker
                (new PhantomTestConfiguration
                 ("testPhantomDupInsertGetNextCommit",
                  "F", "D", "C", "D",
                  true, true, true));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * Phantom Dup test inserting and aborting a phantom while doing a getNext.
     */
    public void testPhantomDupInsertGetNextAbort()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupInsertGetNextAbort",
	      "F", "D", "C", "F",
	      true, true, false));
    }

    /**
     * Phantom Dup test inserting and committing a phantom while doing a
     * getPrev.
     */
    public void testPhantomDupInsertGetPrevCommit()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupInsertGetPrevCommit",
	      "C", "F", "G", "F",
	      true, false, true));
    }

    /**
     * Phantom Dup test inserting and aborting a phantom while doing a getPrev.
     */
    public void testPhantomDupInsertGetPrevAbort()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupInsertGetPrevAbort",
	      "C", "F", "G", "C",
	      true, false, false));
    }

    /**
     * Phantom Dup test deleting and committing an edge element while doing a
     * getNext.
     */
    public void testPhantomDupDeleteGetNextCommit()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupDeleteGetNextCommit",
	      "F", "F", "C", "G",
	      false, true, true));
    }

    /**
     * Phantom Dup test deleting and aborting an edge element while doing a
     * getNext.
     */
    public void testPhantomDupDeleteGetNextAbort()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupDeleteGetNextAbort",
	      "F", "F", "C", "F",
	      false, true, false));
    }

    /**
     * Phantom Dup test deleting and committing an edge element while doing a
     * getPrev.
     */
    public void testPhantomDupDeleteGetPrevCommit()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupDeleteGetPrevCommit",
	      "F", "F", "G", "C",
	      false, false, true));
    }

    /**
     * Phantom Dup test deleting and aborting an edge element while doing a
     * getPrev.
     */
    public void testPhantomDupDeleteGetPrevAbort()
	throws Throwable {

	phantomDupWorker
	    (new PhantomTestConfiguration
	     ("testPhantomDupDeleteGetPrevAbort",
	      "F", "F", "G", "F",
	      false, false, false));
    }

    private void phantomWorker(PhantomTestConfiguration c)
	throws Throwable {

	try {
	    this.config = c;
	    setupDatabaseAndEnv(false);

	    if (config.doInsert &&
		!config.doGetNext) {

		Transaction txnDel =
		    env.beginTransaction(null, TransactionConfig.DEFAULT);

		/*
		 * Delete the first entry in the second bin so that we can
		 * reinsert it in tester1 and have it be the first entry in
		 * that bin.  If we left F and then tried to insert something
		 * to the left of F, it would end up in the first bin.
		 */
		assertEquals(OperationStatus.SUCCESS,
			     db.delete(txnDel,
				       new DatabaseEntry("F".getBytes())));
		txnDel.commit();
	    }

	    JUnitThread tester1 =
		new JUnitThread(config.testName + "1") {
		    public void testBody()
			throws Throwable {

			Cursor cursor = null;
			try {
			    Transaction txn1 =
				env.beginTransaction(null, null);
			    cursor = db.openCursor(txn1, CursorConfig.DEFAULT);
			    OperationStatus status =
				cursor.getSearchKey
				(new DatabaseEntry
				 (config.thread1EntryToLock.getBytes()),
				 new DatabaseEntry(),
				 LockMode.RMW);
			    assertEquals(OperationStatus.SUCCESS, status);
			    sequence++;  // 0 -> 1

			    while (sequence < 2) {
				Thread.yield();
			    }

			    /*
			     * Since we can't increment sequence when tester2
			     * blocks on the getNext call, all we can do is
			     * bump sequence right before the getNext, and then
			     * wait a little in this thread for tester2 to
			     * block.
			     */
			    try {
				Thread.sleep(250);
			    } catch (InterruptedException IE) {
			    }

			    if (config.doInsert) {
				status = db.put
				    (txn1,
				     new DatabaseEntry
				     (config.thread1OpArg.getBytes()),
				     new DatabaseEntry(new byte[10]));
			    } else {
				status = db.delete
				    (txn1,
				     new DatabaseEntry
				     (config.thread1OpArg.getBytes()));
			    }
			    assertEquals(OperationStatus.SUCCESS, status);
			    sequence++;     // 2 -> 3

			    try {
				Thread.sleep(1000);
			    } catch (InterruptedException IE) {
			    }

			    cursor.close();
			    cursor = null;
			    if (config.doCommit) {
				txn1.commit();
			    } else {
				txn1.abort();
			    }
			} catch (DatabaseException DBE) {
			    if (cursor != null) {
				cursor.close();
			    }
			    DBE.printStackTrace();
			    fail("caught DatabaseException " + DBE);
			}
		    }
		};

	    JUnitThread tester2 =
		new JUnitThread(config.testName + "2") {
		    public void testBody()
			throws Throwable {

			Cursor cursor = null;
			try {
			    Transaction txn2 =
				env.beginTransaction(null, null);
			    txn2.setLockTimeout(LOCK_TIMEOUT);
			    cursor = db.openCursor(txn2, CursorConfig.DEFAULT);

			    while (sequence < 1) {
				Thread.yield();
			    }

			    OperationStatus status =
				cursor.getSearchKey
				(new DatabaseEntry
				 (config.thread2Start.getBytes()),
				 new DatabaseEntry(),
				 LockMode.DEFAULT);
			    assertEquals(OperationStatus.SUCCESS, status);

			    sequence++;           // 1 -> 2
			    DatabaseEntry nextKey = new DatabaseEntry();
			    try {

				/*
				 * This will block until tester1 above commits.
				 */
				if (config.doGetNext) {
				    status =
					cursor.getNext(nextKey,
						       new DatabaseEntry(),
						       LockMode.DEFAULT);
				} else {
				    status =
					cursor.getPrev(nextKey,
						       new DatabaseEntry(),
						       LockMode.DEFAULT);
				}
			    } catch (DatabaseException DBE) {
				System.out.println("t2 caught " + DBE);
			    }
			    assertEquals(3, sequence);
			    assertEquals(config.expectedResult,
					 new String(nextKey.getData()));
			    cursor.close();
			    cursor = null;
			    txn2.commit();
			} catch (DatabaseException DBE) {
			    if (cursor != null) {
				cursor.close();
			    }
			    DBE.printStackTrace();
			    fail("caught DatabaseException " + DBE);
			}
		    }
		};

	    tester1.start();
	    tester2.start();

	    tester1.finishTest();
	    tester2.finishTest();
	} finally {
	    db.close();
	    env.close();
            env = null;
	}
    }

    private void phantomDupWorker(PhantomTestConfiguration c)
	throws Throwable {

	Cursor cursor = null;
	try {
	    this.config = c;
	    setupDatabaseAndEnv(true);

	    if (config.doInsert &&
		!config.doGetNext) {

		Transaction txnDel =
		    env.beginTransaction(null, TransactionConfig.DEFAULT);
		cursor = db.openCursor(txnDel, CursorConfig.DEFAULT);

		/*
		 * Delete the first entry in the second bin so that we can
		 * reinsert it in tester1 and have it be the first entry in
		 * that bin.  If we left F and then tried to insert something
		 * to the left of F, it would end up in the first bin.
		 */
		assertEquals(OperationStatus.SUCCESS, cursor.getSearchBoth
			     (new DatabaseEntry(DUPKEY.getBytes()),
			      new DatabaseEntry("F".getBytes()),
			      LockMode.DEFAULT));
		assertEquals(OperationStatus.SUCCESS, cursor.delete());
		cursor.close();
		cursor = null;
		txnDel.commit();
	    }

	    JUnitThread tester1 =
		new JUnitThread(config.testName + "1") {
		    public void testBody()
			throws Throwable {

			Cursor cursor = null;
			Cursor c = null;
			try {
			    Transaction txn1 =
				env.beginTransaction(null, null);
			    cursor = db.openCursor(txn1, CursorConfig.DEFAULT);
			    OperationStatus status =
				cursor.getSearchBoth
				(new DatabaseEntry(DUPKEY.getBytes()),
				 new DatabaseEntry
				 (config.thread1EntryToLock.getBytes()),
				 LockMode.RMW);
			    assertEquals(OperationStatus.SUCCESS, status);
			    cursor.close();
			    cursor = null;
			    sequence++;  // 0 -> 1

			    while (sequence < 2) {
				Thread.yield();
			    }

			    /*
			     * Since we can't increment sequence when tester2
			     * blocks on the getNext call, all we can do is
			     * bump sequence right before the getNext, and then
			     * wait a little in this thread for tester2 to
			     * block.
			     */
			    try {
				Thread.sleep(250);
			    } catch (InterruptedException IE) {
			    }

			    if (config.doInsert) {
				status = db.put
				    (txn1,
				     new DatabaseEntry(DUPKEY.getBytes()),
				     new DatabaseEntry
				     (config.thread1OpArg.getBytes()));
			    } else {
				c = db.openCursor(txn1, CursorConfig.DEFAULT);
				assertEquals(OperationStatus.SUCCESS,
					     c.getSearchBoth
					     (new DatabaseEntry
					      (DUPKEY.getBytes()),
					      new DatabaseEntry
					      (config.thread1OpArg.getBytes()),
					      LockMode.DEFAULT));
				assertEquals(OperationStatus.SUCCESS,
					     c.delete());
				c.close();
				c = null;
			    }
			    assertEquals(OperationStatus.SUCCESS, status);
			    sequence++;     // 2 -> 3

			    try {
				Thread.sleep(1000);
			    } catch (InterruptedException IE) {
			    }

			    if (config.doCommit) {
				txn1.commit();
			    } else {
				txn1.abort();
			    }
			} catch (DatabaseException DBE) {
			    if (cursor != null) {
				cursor.close();
			    }
			    if (c != null) {
				c.close();
			    }
			    DBE.printStackTrace();
			    fail("caught DatabaseException " + DBE);
			}
		    }
		};

	    JUnitThread tester2 =
		new JUnitThread("testPhantomInsert2") {
		    public void testBody()
			throws Throwable {

			Cursor cursor = null;
			try {
			    Transaction txn2 =
				env.beginTransaction(null, null);
			    txn2.setLockTimeout(LOCK_TIMEOUT);
			    cursor = db.openCursor(txn2, CursorConfig.DEFAULT);

			    while (sequence < 1) {
				Thread.yield();
			    }

			    OperationStatus status =
				cursor.getSearchBoth
				(new DatabaseEntry(DUPKEY.getBytes()),
				 new DatabaseEntry
				 (config.thread2Start.getBytes()),
				 LockMode.DEFAULT);
			    assertEquals(OperationStatus.SUCCESS, status);

			    sequence++;           // 1 -> 2
			    DatabaseEntry nextKey = new DatabaseEntry();
			    DatabaseEntry nextData = new DatabaseEntry();
			    try {

				/*
				 * This will block until tester1 above commits.
				 */
				if (config.doGetNext) {
				    status =
					cursor.getNextDup(nextKey, nextData,
							  LockMode.DEFAULT);
				} else {
				    status =
					cursor.getPrevDup(nextKey, nextData,
							  LockMode.DEFAULT);
				}
			    } catch (DatabaseException DBE) {
				System.out.println("t2 caught " + DBE);
			    }
			    assertEquals(3, sequence);
			    byte[] data = nextData.getData();
			    assertEquals(config.expectedResult,
					 new String(data));
			    cursor.close();
			    cursor = null;
			    txn2.commit();
			} catch (DatabaseException DBE) {
			    if (cursor != null) {
				cursor.close();
			    }
			    DBE.printStackTrace();
			    fail("caught DatabaseException " + DBE);
			}
		    }
		};

	    tester1.start();
	    tester2.start();

	    tester1.finishTest();
	    tester2.finishTest();
	} finally {
	    if (cursor != null) {
		cursor.close();
	    }
	    db.close();
	    env.close();
            env = null;
	}
    }

    /**
     * Sets up a small database with a tree containing 2 bins, one with A, B,
     * and C, and the other with F, G, H, and I.
     */
    private void setupDatabaseAndEnv(boolean writeAsDuplicateData)
	throws DatabaseException {

        EnvironmentConfig envConfig = TestUtils.initEnvConfig();

        /* RepeatableRead isolation is required by this test. */
        TestUtils.clearIsolationLevel(envConfig);

	DbInternal.disableParameterValidation(envConfig);
        envConfig.setTransactional(true);
        envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
                                 "6");
        envConfig.setConfigParam(EnvironmentParams.NODE_MAX_DUPTREE.getName(),
                                 "6");
        envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
                                 "1024");
        envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(),
                                 "true");
        envConfig.setAllowCreate(true);
        envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
        env = new Environment(envHome, envConfig);
        Transaction txn = env.beginTransaction(null, null);
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setSortedDuplicates(true);
        dbConfig.setAllowCreate(true);
        db = env.openDatabase(txn, "testDB", dbConfig);

	if (writeAsDuplicateData) {
	    writeDuplicateData(db, txn);
	} else {
	    writeData(db, txn);
	}

	txn.commit();
    }

    String[] dataStrings = {
	"A", "B", "C", "F", "G", "H", "I"
    };

    private void writeData(Database db, Transaction txn)
	throws DatabaseException {

	for (int i = 0; i < dataStrings.length; i++) {
	    db.put(txn, new DatabaseEntry(dataStrings[i].getBytes()),
		   new DatabaseEntry(new byte[10]));
	}
    }

    private void writeDuplicateData(Database db, Transaction txn)
	throws DatabaseException {

	for (int i = 0; i < dataStrings.length; i++) {
	    db.put(txn, new DatabaseEntry(DUPKEY.getBytes()),
		   new DatabaseEntry(dataStrings[i].getBytes()));
	}
    }

    /**
     * Insert data over many databases.
     */
    private void insertMultiDb(int numDbs)
	throws DatabaseException {

        EnvironmentConfig envConfig = TestUtils.initEnvConfig();

        /* RepeatableRead isolation is required by this test. */
        TestUtils.clearIsolationLevel(envConfig);

	DbInternal.disableParameterValidation(envConfig);
        envConfig.setTransactional(true);
        envConfig.setConfigParam
	    (EnvironmentParams.LOG_FILE_MAX.getName(), "1024");
        envConfig.setConfigParam
	    (EnvironmentParams.ENV_CHECK_LEAKS.getName(), "true");
	envConfig.setConfigParam
	    (EnvironmentParams.NODE_MAX.getName(), "6");
        envConfig.setConfigParam
	    (EnvironmentParams.NODE_MAX_DUPTREE.getName(), "6");
        envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
        envConfig.setAllowCreate(true);
        Environment env = new Environment(envHome, envConfig);

        Database[] myDb = new Database[numDbs];
        Cursor[] cursor = new Cursor[numDbs];
        Transaction txn =
	    env.beginTransaction(null, TransactionConfig.DEFAULT);

        /* In a non-replicated environment, the txn id should be positive. */
        assertTrue(txn.getId() > 0);

        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(true);
        for (int i = 0; i < numDbs; i++) {
            myDb[i] = env.openDatabase(txn, "testDB" + i, dbConfig);
            cursor[i] = myDb[i].openCursor(txn, CursorConfig.DEFAULT);

            /*
             * In a non-replicated environment, the db id should be
             * positive.
             */
            DatabaseImpl dbImpl = DbInternal.dbGetDatabaseImpl(myDb[i]);
            assertTrue(dbImpl.getId().getId() > 0);
        }

        /* Insert data in a round robin fashion to spread over log. */
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        for (int i = NUM_RECS; i > 0; i--) {
            for (int c = 0; c < numDbs; c++) {
                key.setData(TestUtils.getTestArray(i + c));
                data.setData(TestUtils.getTestArray(i + c));
                if (DEBUG) {
                    System.out.println("i = " + i +
                                       TestUtils.dumpByteArray(key.getData()));
                }
                cursor[c].put(key, data);
            }
        }

        for (int i = 0; i < numDbs; i++) {
            cursor[i].close();
            myDb[i].close();
        }
        txn.commit();

        assertTrue(env.verify(null, System.err));
        env.close();
        env = null;

        envConfig.setAllowCreate(false);
        env = new Environment(envHome, envConfig);

        /*
         * Before running the verifier, run the cleaner to make sure it has
         * completed.  Otherwise, the cleaner will be running when we call
         * verify, and open txns will be reported.
         */
        env.cleanLog();

        env.verify(null, System.err);

        /* Check each db in turn, using null transactions. */
        dbConfig.setTransactional(false);
        dbConfig.setAllowCreate(false);
        for (int d = 0; d < numDbs; d++) {
            Database checkDb = env.openDatabase(null, "testDB" + d,
						dbConfig);
            Cursor myCursor = checkDb.openCursor(null, CursorConfig.DEFAULT);

            OperationStatus status =
		myCursor.getFirst(key, data, LockMode.DEFAULT);

            int i = 1;
            while (status == OperationStatus.SUCCESS) {
                byte[] expectedKey = TestUtils.getTestArray(i + d);
                byte[] expectedData = TestUtils.getTestArray(i + d);

                if (DEBUG) {
                    System.out.println("Database " + d + " Key " + i +
                                       " expected = " +
                                       TestUtils.dumpByteArray(expectedKey) +
                                       " seen = " +
                                       TestUtils.dumpByteArray(key.getData()));
                }

                assertTrue("Database " + d + " Key " + i + " expected = " +
                           TestUtils.dumpByteArray(expectedKey) +
                           " seen = " +
                           TestUtils.dumpByteArray(key.getData()),
                           Arrays.equals(expectedKey, key.getData()));
                assertTrue("Data " + i, Arrays.equals(expectedData,
                                                      data.getData()));
                i++;

                status = myCursor.getNext(key, data, LockMode.DEFAULT);
            }
	    myCursor.close();
            assertEquals("Number recs seen", NUM_RECS, i-1);
            checkDb.close();
        }
        env.close();
        env = null;
    }
}