1 8 9 package com.sleepycat.je.test; 10 11 import junit.framework.Test; 12 13 import com.sleepycat.je.Cursor; 14 import com.sleepycat.je.Database; 15 import com.sleepycat.je.DatabaseConfig; 16 import com.sleepycat.je.DatabaseEntry; 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.DeadlockException; 19 import com.sleepycat.je.LockMode; 20 import com.sleepycat.je.OperationStatus; 21 import com.sleepycat.je.Transaction; 22 import com.sleepycat.je.junit.JUnitMethodThread; 23 import com.sleepycat.je.junit.JUnitThread; 24 import com.sleepycat.je.util.TestUtils; 25 26 96 public class AtomicPutTest extends TxnTestCase { 97 98 private static final int MAX_KEY = 400; 100 public static Test suite() { 101 return txnTestSuite(AtomicPutTest.class, null, 102 new String [] {TxnTestCase.TXN_USER}); 104 } 105 106 private int nextKey; 107 private Database db; 108 109 112 public void tearDown() 113 throws Exception { 114 115 if (db != null) { 116 try { 117 db.close(); 118 } catch (Exception e) {} 119 db = null; 120 } 121 super.tearDown(); 122 } 123 124 128 public void testOverwriteNoDuplicates() 129 throws Throwable { 130 131 String method = "runOverwriteNoDuplicates"; 132 JUnitMethodThread tester1 = new JUnitMethodThread(method + "-t1", 133 method, this); 134 JUnitMethodThread tester2 = new JUnitMethodThread(method + "-t2", 135 method, this); 136 db = openDb("foo", false); 137 tester1.start(); 138 tester2.start(); 139 finishTests(new JUnitThread[] { tester1, tester2 }); 140 db.close(); 141 db = null; 142 } 143 144 153 public void runOverwriteNoDuplicates() 154 throws DatabaseException { 155 156 DatabaseEntry key = new DatabaseEntry(); 157 DatabaseEntry data = new DatabaseEntry(); 158 while (nextKey < MAX_KEY) { 159 164 int val = nextKey++ / 2; 165 Transaction txn = txnBegin(); 166 key.setData(TestUtils.getTestArray(val)); 167 data.setData(TestUtils.getTestArray(val)); 168 boolean commit = true; 169 try { 170 OperationStatus status = db.put(txn, key, data); 171 assertEquals("Key=" + val, OperationStatus.SUCCESS, status); 172 } catch (DeadlockException DE) { 173 commit = false; 174 } 175 if (commit) { 176 txnCommit(txn); 177 } else { 178 txnAbort(txn); 179 } 180 } 181 } 182 183 187 public void testNoOverwriteWithDuplicates() 188 throws Throwable { 189 190 String method = "runNoOverwriteWithDuplicates"; 191 JUnitMethodThread tester1 = new JUnitMethodThread(method + "-t1", 192 method, this); 193 JUnitMethodThread tester2 = new JUnitMethodThread(method + "-t2", 194 method, this); 195 db = openDb("foo", true); 196 tester1.start(); 197 tester2.start(); 198 finishTests(new JUnitThread[] { tester1, tester2 }); 199 db.close(); 200 db = null; 201 } 202 203 210 public void runNoOverwriteWithDuplicates() 211 throws DatabaseException { 212 213 DatabaseEntry key = new DatabaseEntry(); 214 DatabaseEntry data = new DatabaseEntry(); 215 while (nextKey < MAX_KEY) { 216 222 int val = nextKey++; 223 int keyVal = val / 2; 224 int dataVal = val % 2; 225 key.setData(TestUtils.getTestArray(keyVal)); 226 data.setData(TestUtils.getTestArray(dataVal)); 227 while (true) { 228 Transaction txn = txnBegin(); 229 boolean commit = true; 230 try { 231 db.putNoOverwrite(txn, key, data); 232 } catch (DeadlockException DE) { 233 commit = false; 234 } 235 if (commit) { 236 txnCommit(txn); 237 break; 238 } else { 239 txnAbort(txn); 240 } 241 } 242 Cursor cursor = db.openCursor(null, null); 243 try { 244 OperationStatus status = cursor.getSearchKey(key, data, 245 LockMode.DEFAULT); 246 assertEquals(OperationStatus.SUCCESS, status); 247 assertEquals(1, cursor.count()); 248 } finally { 249 cursor.close(); 250 } 251 } 252 } 253 254 257 private Database openDb(String name, boolean dups) 258 throws DatabaseException { 259 260 DatabaseConfig dbConfig = new DatabaseConfig(); 261 dbConfig.setTransactional(isTransactional); 262 dbConfig.setAllowCreate(true); 263 dbConfig.setSortedDuplicates(dups); 264 265 Transaction txn = txnBegin(); 266 try { 267 return env.openDatabase(txn, name, dbConfig); 268 } finally { 269 txnCommit(txn); 270 } 271 } 272 273 277 private void finishTests(JUnitThread[] threads) 278 throws Throwable { 279 280 Throwable ex = null; 281 for (int i = 0; i < threads.length; i += 1) { 282 try { 283 threads[i].finishTest(); 284 } catch (Throwable e) { 285 if (ex == null) { 286 ex = e; 287 } 288 } 289 } 290 if (ex != null) { 291 throw ex; 292 } 293 } 294 } 295 | Popular Tags |