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.db4ounit.common.staging;
022:
023: import com.db4o.config.*;
024: import com.db4o.db4ounit.common.cs.*;
025: import com.db4o.foundation.*;
026: import com.db4o.internal.cs.*;
027: import com.db4o.internal.cs.messages.*;
028:
029: import db4ounit.*;
030:
031: /**
032: * @exclude
033: */
034: public class ClientServerPingTestCase extends ClientServerTestCaseBase {
035:
036: private static final int ITEM_COUNT = 100;
037:
038: public static void main(String[] arguments) {
039: new ClientServerPingTestCase().runClientServer();
040: }
041:
042: protected void configure(Configuration config) {
043: config.clientServer().batchMessages(false);
044: }
045:
046: public void test() throws Exception {
047: if (isMTOC()) {
048: // This test really doesn't make sense for MTOC, there
049: // is no client to ping.
050: return;
051: }
052: ServerMessageDispatcher dispatcher = serverDispatcher();
053: PingThread pingThread = new PingThread(dispatcher);
054: pingThread.start();
055: for (int i = 0; i < ITEM_COUNT; i++) {
056: Item item = new Item(i);
057: store(item);
058: }
059: Assert.areEqual(ITEM_COUNT, db().get(Item.class).size());
060: pingThread.close();
061: }
062:
063: public static class Item {
064:
065: public int data;
066:
067: public Item(int i) {
068: data = i;
069: }
070:
071: }
072:
073: static class PingThread extends Thread {
074:
075: ServerMessageDispatcher _dispatcher;
076: boolean _stop;
077:
078: private final Object lock = new Object();
079:
080: public PingThread(ServerMessageDispatcher dispatcher) {
081: _dispatcher = dispatcher;
082: }
083:
084: public void close() {
085: synchronized (lock) {
086: _stop = true;
087: }
088: }
089:
090: private boolean notStopped() {
091: synchronized (lock) {
092: return !_stop;
093: }
094: }
095:
096: public void run() {
097: while (notStopped()) {
098: _dispatcher.write(Msg.PING);
099: Cool.sleepIgnoringInterruption(1);
100: }
101: }
102: }
103:
104: }
|