File: APILockoutTest.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 (383 lines) | stat: -rw-r--r-- 9,392 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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: APILockoutTest.java,v 1.10.2.2 2010/01/04 15:30:43 cwl Exp $
 */

package com.sleepycat.je.dbi;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

import com.sleepycat.je.APILockedException;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.junit.JUnitThread;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.txn.BasicLocker;
import com.sleepycat.je.util.TestUtils;

public class APILockoutTest extends TestCase {
    private File envHome;

    private JUnitThread tester1 = null;
    private JUnitThread tester2 = null;

    private volatile int flag = 0;

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

    public void setUp() throws IOException, DatabaseException {
        TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
    }

    public void tearDown() throws IOException, DatabaseException {
        TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
    }

    public void testBasic()
	throws Throwable {

	EnvironmentConfig envConfig = TestUtils.initEnvConfig();
	envConfig.setTransactional(true);
	envConfig.setAllowCreate(true);
	envConfig.setConfigParam
	    (EnvironmentParams.ENV_LOCKOUT_TIMEOUT.getName(), "1000");
	Environment env = new Environment(envHome, envConfig);
	final EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
	envImpl.setupAPILock();
	envImpl.acquireAPIWriteLock(1, TimeUnit.SECONDS);

	tester1 =
	    new JUnitThread("testWait-Thread1") {
		public void testBody()
		    throws DatabaseException {

		    BasicLocker bl = BasicLocker.
			createBasicLocker(envImpl, false /*noWait*/,
					  true /*noAPIReadLock*/);
		    try {
			envImpl.acquireAPIReadLock(bl);
			fail("expected timeout");
		    } catch (Exception E) {
			assertTrue(E instanceof APILockedException);
		    }
		    bl.operationEnd(false);
		    try {
			bl = BasicLocker.
			    createBasicLocker(envImpl,
					      false /*noWait*/,
					      false /*noAPIReadLock*/);
			fail("expected timeout");
		    } catch (Exception E) {
			// expected
		    }
		    flag = 1;
		}
	    };

	tester2 =
	    new JUnitThread("testWait-Thread2") {
		public void testBody()
		    throws DatabaseException {

		    while (flag < 2) {
			Thread.yield();
		    }
		    BasicLocker bl =
			BasicLocker.createBasicLocker(envImpl,
						      false /*noWait*/,
						      true /*noAPIReadLock*/);
		    try {
			envImpl.acquireAPIReadLock(bl);
		    } catch (Exception E) {
			E.printStackTrace();
			fail("expected success");
		    }

		    envImpl.releaseAPIReadLock(bl);

		    /* Second release should succeed -- we're not checking. */
		    try {
			envImpl.releaseAPIReadLock(bl);
		    } catch (IllegalMonitorStateException IMSE) {
			fail("expected success");
		    }
		    bl.operationEnd(true);
		}
	    };

	tester1.start();
	tester2.start();
	/* Wait for acquireAPIReadLock to complete. */
	while (flag < 1) {
	    Thread.yield();
	}

	/*
	 * Make sure that write locking thread (main) can't read lock, too.
	 */
	try {
	    BasicLocker bl =
		BasicLocker.createBasicLocker(envImpl, false /*noWait*/,
					      true /*noAPIReadLock*/);
	    envImpl.acquireAPIReadLock(bl);
	    fail("expected exception");
	} catch (DatabaseException DE) {
	    /* ignore */
	}

	envImpl.releaseAPIWriteLock();
	flag = 2;
	tester1.finishTest();
	tester2.finishTest();
	try {

	    /*
	     * Expect an IllegalMonitorStateException saying that environment
	     * is not currently locked.
	     */
	    envImpl.releaseAPIWriteLock();
	    fail("expected exception");
	} catch (IllegalMonitorStateException IMSE) {
	    /* Ignore */
	}
	env.close();
    }

    enum BlockingOperation {
	TRANSACTION, CURSOR_OPEN, TRANSACTION_WITH_CURSOR,
	NON_TRANSACTIONAL_DB_PUT, TRANSACTIONAL_DB_PUT, CURSOR_PUT
    };

    public void testTransactionBlocking()
	throws Throwable {

	doBlockingTest(BlockingOperation.TRANSACTION);
    }

    public void testCursorWithNullTransactionBlocking()
	throws Throwable {

	doBlockingTest(BlockingOperation.CURSOR_OPEN);
    }

    public void testCursorWithTransactionBlocking()
	throws Throwable {

	doBlockingTest(BlockingOperation.TRANSACTION_WITH_CURSOR);
    }

    public void testDbPutWithNullTransactionBlocking()
	throws Throwable {

	doBlockingTest(BlockingOperation.NON_TRANSACTIONAL_DB_PUT);
    }

    public void testDbPutWithTransactionBlocking()
	throws Throwable {

	doBlockingTest(BlockingOperation.TRANSACTIONAL_DB_PUT);
    }

    public void testCursorPut()
	throws Throwable {

	doBlockingTest(BlockingOperation.CURSOR_PUT);
    }

    private void doBlockingTest(final BlockingOperation operation)
	throws Throwable {

	EnvironmentConfig envConfig = TestUtils.initEnvConfig();
	envConfig.setTransactional(true);
	envConfig.setAllowCreate(true);
	envConfig.setConfigParam
	    (EnvironmentParams.ENV_LOCKOUT_TIMEOUT.getName(), "1000");
	final Environment env = new Environment(envHome, envConfig);
	final EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
	DatabaseConfig dbConf = new DatabaseConfig();
	dbConf.setTransactional(true);
	dbConf.setAllowCreate(true);

	envImpl.setupAPILock();

	final Database db = env.openDatabase(null, "foo", dbConf);

	envImpl.acquireAPIWriteLock(1, TimeUnit.SECONDS);

	tester1 =
	    new JUnitThread("testWait-Thread1") {
		public void testBody()
		    throws DatabaseException {

		    Transaction txn = null;
		    Cursor cursor = null;
		    DatabaseEntry key = new DatabaseEntry();
		    DatabaseEntry data = new DatabaseEntry();
		    key.setData(new byte[] { 0, 1 });
		    data.setData(new byte[] { 0, 1 });
		    /* Try to do opn. while api is locked.  Should fail. */
		    try {
			switch (operation) {
			case CURSOR_OPEN:
			    cursor = db.openCursor(null, null);
			    break;

			case TRANSACTION:
			case TRANSACTION_WITH_CURSOR:
			    txn = env.beginTransaction(null, null);
			    break;

			case NON_TRANSACTIONAL_DB_PUT:
			    db.put(null, key, data);
			    break;

			case TRANSACTIONAL_DB_PUT:
			case CURSOR_PUT:
			    throw new DatabaseException("fake DE");
			}
			fail("expected timeout");
		    } catch (DatabaseException DE) {
			/* Ignore. */
		    }

		    flag = 1;

		    /* Wait for main to unlock the API, then do operation. */
		    while (flag < 2) {
			Thread.yield();
		    }
		    try {
			switch (operation) {
			case CURSOR_OPEN:
			    cursor = db.openCursor(null, null);
			    break;

			case TRANSACTION:
			case TRANSACTION_WITH_CURSOR:
			case CURSOR_PUT:
			    txn = env.beginTransaction(null, null);
			    if (operation ==
				BlockingOperation.TRANSACTION_WITH_CURSOR ||
				operation ==
				BlockingOperation.CURSOR_PUT) {
				cursor = db.openCursor(txn, null);
				if (operation ==
				    BlockingOperation.CURSOR_PUT) {
				    cursor.put(key, data);
				}
			    }
			    break;

			case NON_TRANSACTIONAL_DB_PUT:
			    db.put(null, key, data);
			    break;

			case TRANSACTIONAL_DB_PUT:
			    txn = env.beginTransaction(null, null);
			    db.put(txn, key, data);
			}
		    } catch (Exception E) {
			fail("expected success");
		    }

		    /* Return control to main. */
		    flag = 3;

		    /* Wait for main to attempt lock on the API (and fail). */
		    while (flag < 4) {
			Thread.yield();
		    }
		    try {
			switch (operation) {
			case CURSOR_OPEN:
			    cursor.close();
			    break;

			case TRANSACTION:
			case TRANSACTION_WITH_CURSOR:
			case TRANSACTIONAL_DB_PUT:
			case CURSOR_PUT:
			    if (operation ==
				BlockingOperation.TRANSACTION_WITH_CURSOR ||
				operation ==
				BlockingOperation.CURSOR_PUT) {
				cursor.close();
			    }
			    if (txn == null) {
				fail("txn is null");
			    }
			    txn.abort();
			    break;

			case NON_TRANSACTIONAL_DB_PUT:
			    /* Do nothing. */
			    break;
			}
		    } catch (Exception E) {
			fail("expected success");
		    }

		    flag = 5;
		}
	    };

	tester1.start();
	/* Wait for acquireAPIReadLock to complete. */
	while (flag < 1) {
	    Thread.yield();
	}
	envImpl.releaseAPIWriteLock();
	flag = 2;

	/* Wait for tester1 to begin a txn. */
	while (flag < 3) {
	    Thread.yield();
	}

	if (operation == BlockingOperation.TRANSACTION ||
	    operation == BlockingOperation.TRANSACTION_WITH_CURSOR) {
	    /* Attempt lock.  Should timeout. */
	    try {
		envImpl.acquireAPIWriteLock(1, TimeUnit.SECONDS);
		fail("expected timeout.");
	    } catch (DatabaseException DE) {
		/* Ignore.  Expect timeout. */
	    }
	}

	/* Back to tester1 to end the txn. */
	flag = 4;
	while (flag < 5) {
	    Thread.yield();
	}

	/* Attempt lock.  Should complete. */
	try {
	    envImpl.acquireAPIWriteLock(1, TimeUnit.SECONDS);
	} catch (DatabaseException DE) {
	    fail("expected success.");
	}

	tester1.finishTest();
	envImpl.releaseAPIWriteLock();
	db.close();
	env.close();
    }
}