Ignore:
Timestamp:
2015-09-28T11:08:25+13:00 (9 years ago)
Author:
jmt12
Message:

Adding in tests to open multiple connections to the same database (inode) at the same time. This triggered issues within GSDL3 due to the tdb library itself erroring out if the same process tries to open the same database more than once. Note that multiple opens from different processes is fine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tdb/trunk/src/java/org/greenstone/tdbjava/TDBJavaTest.java

    r30193 r30274  
    162162
    163163    try {
    164     // Create a simple database
    165     TDBJava db = new TDBJava(db_path);
    166     Test.ok(1, db != null);
    167 
    168     // Check database is open
    169     Test.ok(1.5, db.isOpen());
    170 
    171     // Store some data in the file
    172     db.store("mountain", "Kosciusko");
    173     Test.ok(2, true);
    174 
    175     // Store and read back data
    176     db.store("abc", "ABC");
    177     value = db.fetch("abc");
    178     Test.ok(3, value.equals("ABC"));
    179 
    180     // Check whether keys exist or not
    181     Test.ok(4, db.exists("abc"));
    182     Test.ok(5, !db.exists("jimmy"));
    183     Test.ok(6, !db.exists("abc\0"));
    184 
    185     // Delete those records
    186     db.delete("mountain");
    187     Test.ok(9, !db.exists("mountain"));
    188     db.delete("abc");
    189     Test.ok(10, !db.exists("abc"));
    190 
    191     // Store a series of records in the file
    192     for (int i = 100; i < 200; i++) {
    193         db.store(Integer.toString(i, Character.MAX_RADIX),
    194              Integer.toString(i, Character.MIN_RADIX));
    195     }
    196 
    197     // Enumerate keys and values out of the file
    198     int count = 0;
    199     Enumeration keys = db.keys();
    200     while (keys.hasMoreElements()) {
    201         key = (String) keys.nextElement();
    202         // System.out.print(key + "=");
    203         // value = (String) db.fetch(key);
    204         // System.out.println(value);
    205         count++;
    206     }
    207     Test.ok(20, count==100);
    208 
    209     // Delete all the records from the file
    210     count = 0;
    211     while ((key = (String) db.getFirstKey()) != null) {
    212         db.delete(key);
    213         count++;
    214     }
    215     Test.ok(30, count==100);
    216 
    217     // Try to fetch a non-existent key
    218     boolean caught = false;
    219     try {
    220         db.fetch("larry!");
    221     }
    222     catch (Exception e) {
    223         // TODO: Check the exception was the one we expected?
    224         System.out.println("caught: " + e.toString());
    225         caught = true;
    226     }
    227     Test.ok(40, caught);
     164        // Create a simple database
     165        TDBJava db = TDBJava.getConnection(db_path);
     166        Test.ok(1, db != null);
     167
     168        // Check database is open
     169        Test.ok(1.5, db.isOpen());
     170
     171        // Store some data in the file
     172        db.store("mountain", "Kosciusko");
     173        Test.ok(2, true);
     174
     175        // Store and read back data
     176        db.store("abc", "ABC");
     177        value = db.fetch("abc");
     178        Test.ok(3, value.equals("ABC"));
     179
     180        // Check whether keys exist or not
     181        Test.ok(4, db.exists("abc"));
     182        Test.ok(5, !db.exists("jimmy"));
     183        Test.ok(6, !db.exists("abc\0"));
     184
     185        // Open a second handle to the same database
     186        TDBJava db4 = TDBJava.getConnection(db_path);
     187        Test.ok(701, db4 != null);
     188        // read an existing key
     189        value = db4.fetch("abc");
     190        Test.ok(702, value.equals("ABC"));
     191        // write a new key
     192        db4.store("breakfast", "Bacon");
     193        Test.ok(703, true);
     194        // read it out
     195        value = db4.fetch("breakfast");
     196        Test.ok(704, value.equals("Bacon"));
     197        // delete a record
     198        db4.delete("mountain");
     199        Test.ok(705, !db4.exists("mountain"));
     200        // close it
     201        db4.close();
     202        // test added record still exists
     203        value = db.fetch("breakfast");
     204        Test.ok(706, value.equals("Bacon"));
     205
     206        // Delete those records
     207        db.delete("breakfast");
     208        Test.ok(9, !db.exists("breakfast"));
     209        db.delete("abc");
     210        Test.ok(10, !db.exists("abc"));
     211
     212        // Store a series of records in the file
     213        for (int i = 100; i < 200; i++) {
     214        db.store(Integer.toString(i, Character.MAX_RADIX),
     215             Integer.toString(i, Character.MIN_RADIX));
     216        }
     217
     218        // Enumerate keys and values out of the file
     219        int count = 0;
     220        Enumeration keys = db.keys();
     221        while (keys.hasMoreElements()) {
     222        key = (String) keys.nextElement();
     223        // System.out.print(key + "=");
     224        // value = (String) db.fetch(key);
     225        // System.out.println(value);
     226        count++;
     227        }
     228        Test.ok(20, count==100);
     229
     230        // Delete all the records from the file
     231        count = 0;
     232        while ((key = (String) db.getFirstKey()) != null) {
     233        db.delete(key);
     234        count++;
     235        }
     236        Test.ok(30, count==100);
     237
     238        // Try to fetch a non-existent key
     239        boolean caught = false;
     240        try {
     241        db.fetch("larry!");
     242        }
     243        catch (Exception e) {
     244        // TODO: Check the exception was the one we expected?
     245        ///ystem.out.println("caught: " + e.toString());
     246        caught = true;
     247        }
     248        Test.ok(40, caught);
    228249
    229250    // Store some very large data
     
    248269    }
    249270    catch (Exception e) {
    250         System.out.println("caught: " + e.toString());
     271        ///ystem.out.println("caught: " + e.toString());
    251272        caught = true;
    252273    }
    253274    Test.ok(70, caught);
    254275
     276    /* No longer applicable
    255277    // Try to open a non-existent database
    256278    String bogus_db_path = "/tmp/somenonexistentdatabase.tdb";
     
    258280    caught = false;
    259281    try {
    260         db = new TDBJava(bogus_db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
     282        db = TDBJava.getConnection(bogus_db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
     283    }
     284    catch (Exception e) {
     285        ///ystem.out.println("caught: " + e.toString());
     286        caught = true;
     287    }
     288    Test.ok(80, caught);
     289
     290    // Open a database read-only
     291    db = TDBJava.getConnection(db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
     292    Test.ok(90);
     293
     294    // Try to insert
     295    caught = false;
     296    try {
     297        db.store("apple", "crumble");
    261298    }
    262299    catch (Exception e) {
     
    264301        caught = true;
    265302    }
    266     Test.ok(80, caught);
    267 
    268     // Open a database read-only
    269     db = new TDBJava(db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
    270     Test.ok(90);
    271 
    272     // Try to insert
    273     caught = false;
    274     try {
    275         db.store("apple", "crumble");
    276     }
    277     catch (Exception e) {
    278         System.out.println("caught: " + e.toString());
    279         caught = true;
    280     }
    281303    Test.ok(91, caught);
    282304
    283305    db.close();
     306    */
    284307
    285308    // Replace an existing database
    286     db = new TDBJava(db_path, TDBJava.TDB_CLEAR_IF_FIRST);
     309    db = TDBJava.newConnection(db_path);
    287310    Test.ok(100, !db.exists("mountain"));
    288311
     
    290313    final String db_path2 = "/tmp/java2.tdb";
    291314    ensureNoDatabase(db_path2);
    292     TDBJava db2 = new TDBJava(db_path2);
     315    TDBJava db2 = TDBJava.getConnection(db_path2);
    293316    final String db_path3 = "/tmp/java3.tdb";
    294317    ensureNoDatabase(db_path3);
    295     TDBJava db3 = new TDBJava(db_path3);
     318    TDBJava db3 = TDBJava.getConnection(db_path3);
    296319    Test.ok(110, true);
    297320
Note: See TracChangeset for help on using the changeset viewer.