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.jre12.assorted;
022:
023: import com.db4o.*;
024: import com.db4o.db4ounit.util.*;
025: import com.db4o.internal.cs.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030: import db4ounit.extensions.fixtures.*;
031:
032: public class ClientProcessesTestCase extends AbstractDb4oTestCase
033: implements OptOutAllButNetworkingCS {
034:
035: public static final int ITEM_COUNT = 10;
036:
037: public static final String CLIENT_STARTED_OK = "[STARTED]";
038:
039: public static final String CLIENT_COMPLETED_OK = "[COMPLETED]";
040:
041: public static void main(String[] args) {
042: new ClientProcessesTestCase().runClientServer();
043: }
044:
045: public void _testMassiveClientConnect() throws InterruptedException {
046:
047: final int CLIENT_COUNT = 20; // more than 200 clients will need more than 3 GB of memory
048:
049: final StringBuffer results = new StringBuffer();
050:
051: ThreadServices.spawnAndJoin(CLIENT_COUNT, new CodeBlock() {
052: public void run() throws Throwable {
053: String result = JavaServices
054: .java(clientRunnerCommand());
055: results.append(result);
056: Assert.isTrue(result.indexOf(CLIENT_COMPLETED_OK) >= 0);
057: }
058: });
059: System.out.println(results);
060: asserItemCount(CLIENT_COUNT * ITEM_COUNT);
061: }
062:
063: public void _testKillingClients() throws InterruptedException {
064:
065: final int CLIENT_COUNT = 3;
066:
067: final StringBuffer results = new StringBuffer();
068:
069: ThreadServices.spawnAndJoin(CLIENT_COUNT, new CodeBlock() {
070: public void run() throws Throwable {
071: results.append(JavaServices
072: .startAndKillJavaProcess(clientRunnerCommand(),
073: CLIENT_STARTED_OK, 10000));
074: }
075: });
076:
077: Assert.areEqual(1, connectedClients());
078: System.out.println(results);
079: }
080:
081: private int connectedClients() {
082: Db4oClientServer csFixture = (Db4oClientServer) fixture();
083: return (csFixture.server()).ext().clientCount();
084: }
085:
086: private void asserItemCount(final int expectedCount) {
087: Query query = db().query();
088: query.constrain(Item.class);
089: int itemCount = query.execute().size();
090: Assert.areEqual(expectedCount, itemCount);
091: }
092:
093: String clientRunnerCommand() {
094: return ClientRunner.class.getName() + " "
095: + ((Db4oClientServer) fixture()).serverPort();
096: }
097:
098: public static class ClientRunner {
099:
100: private final int _port;
101:
102: private ClientRunner(int port) {
103: _port = port;
104: }
105:
106: public static void main(String[] arguments) {
107: if (arguments == null || arguments.length == 0) {
108: return;
109: }
110: int port = new Integer(arguments[0]).intValue();
111: new ClientRunner(port).start();
112: }
113:
114: private void start() {
115: ObjectContainer oc = Db4o.openClient(Db4oClientServer.HOST,
116: _port, Db4oClientServer.USERNAME,
117: Db4oClientServer.PASSWORD);
118: oc.set(new Item(0));
119: oc.commit();
120: print("[0]");
121: print(CLIENT_STARTED_OK);
122: for (int i = 1; i < ITEM_COUNT; i++) {
123: oc.set(new Item(i));
124: oc.commit();
125: print("[" + i + "]");
126: }
127: oc.close();
128: print(CLIENT_COMPLETED_OK);
129: }
130:
131: private void print(String str) {
132: System.out.println(str);
133: }
134:
135: }
136:
137: public static class Item {
138:
139: public int _number;
140:
141: public Item(int number) {
142: _number = number;
143: }
144:
145: }
146:
147: }
|