001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test;
022:
023: import com.db4o.ext.*;
024: import com.db4o.foundation.*;
025:
026: public class SetSemaphore {
027:
028: public void test() throws InterruptedException {
029:
030: final ExtObjectContainer[] clients = new ExtObjectContainer[5];
031:
032: clients[0] = Test.objectContainer();
033:
034: Test.ensure(clients[0].setSemaphore("hi", 0));
035: Test.ensure(clients[0].setSemaphore("hi", 0));
036:
037: if (Test.clientServer) {
038: for (int i = 1; i < clients.length; i++) {
039: clients[i] = Test.open();
040: }
041:
042: Test.ensure(!clients[1].setSemaphore("hi", 0));
043: clients[0].releaseSemaphore("hi");
044: Test.ensure(clients[1].setSemaphore("hi", 50));
045: Test.ensure(!clients[0].setSemaphore("hi", 0));
046: Test.ensure(!clients[2].setSemaphore("hi", 0));
047:
048: Thread[] threads = new Thread[clients.length];
049:
050: for (int i = 0; i < clients.length; i++) {
051: threads[i] = startGetAndReleaseThread(clients[i]);
052: }
053:
054: for (int i = 0; i < threads.length; i++) {
055: threads[i].join();
056: }
057:
058: Cool.sleepIgnoringInterruption(50);
059:
060: Test.ensure(clients[0].setSemaphore("hi", 0));
061: clients[0].close();
062:
063: threads[2] = startGetAndReleaseThread(clients[2]);
064: threads[1] = startGetAndReleaseThread(clients[1]);
065:
066: threads[1].join();
067: threads[2].join();
068:
069: for (int i = 1; i < 4; i++) {
070: clients[i].close();
071: }
072:
073: clients[4].setSemaphore("hi", 1000);
074: }
075:
076: }
077:
078: private Thread startGetAndReleaseThread(ExtObjectContainer client) {
079: Thread t = new Thread(new GetAndRelease(client));
080: t.start();
081: return t;
082: }
083:
084: static class GetAndRelease implements Runnable {
085:
086: ExtObjectContainer _client;
087:
088: public GetAndRelease(ExtObjectContainer client) {
089: this ._client = client;
090: }
091:
092: public void run() {
093: long time = System.currentTimeMillis();
094: Test.ensure(_client.setSemaphore("hi", 50000));
095: time = System.currentTimeMillis() - time;
096: // System.out.println("Time to get semaphore: " + time);
097: Cool.sleepIgnoringInterruption(50);
098:
099: // System.out.println("About to release semaphore.");
100: _client.releaseSemaphore("hi");
101: }
102: }
103:
104: }
|