diff options
-rw-r--r-- | java/test/Freeze/evictor/Client.java | 351 | ||||
-rw-r--r-- | java/test/Freeze/evictor/RemoteEvictorFactoryI.java | 68 | ||||
-rw-r--r-- | java/test/Freeze/evictor/RemoteEvictorI.java | 71 | ||||
-rw-r--r-- | java/test/Freeze/evictor/ServantI.java | 65 | ||||
-rw-r--r-- | java/test/Freeze/evictor/Server.java | 97 | ||||
-rw-r--r-- | java/test/Freeze/evictor/Test.ice | 51 | ||||
-rw-r--r-- | java/test/Freeze/evictor/build.xml | 44 | ||||
-rwxr-xr-x | java/test/Freeze/evictor/run.py | 30 |
8 files changed, 777 insertions, 0 deletions
diff --git a/java/test/Freeze/evictor/Client.java b/java/test/Freeze/evictor/Client.java new file mode 100644 index 00000000000..5b4197cb300 --- /dev/null +++ b/java/test/Freeze/evictor/Client.java @@ -0,0 +1,351 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +public class Client +{ + private static void + test(boolean b) + { + if(!b) + { + throw new RuntimeException(); + } + } + + private static int + run(String[] args, Ice.Communicator communicator) + { + String ref = "factory:default -p 12345 -t 2000"; + Ice.ObjectPrx base = communicator.stringToProxy(ref); + test(base != null); + Test.RemoteEvictorFactoryPrx factory = Test.RemoteEvictorFactoryPrxHelper.checkedCast(base); + + // + // Test SaveUponEviction mode + // + { + System.out.print("testing SaveUponEviction mode... "); + System.out.flush(); + + final int size = 5; + + Test.RemoteEvictorPrx evictor = + factory.createEvictor("saveUponEviction", Test.EvictorPersistenceMode.SaveUponEviction); + evictor.setSize(size); + + // + // Create the same number of servants as the evictor size + // (i.e., don't exceed queue size). Servants should be + // saved immediately. + // + Test.ServantPrx[] servants = new Test.ServantPrx[size]; + for(int i = 0; i < size; i++) + { + servants[i] = evictor.createServant(i); + test(evictor.getLastSavedValue() == i); + } + + // + // Evict and verify values. + // + evictor.setSize(0); + evictor.setSize(size); + evictor.clearLastSavedValue(); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i); + } + + // + // Mutate servants. + // + for(int i = 0; i < size; i++) + { + servants[i].setValue(i + 100); + } + + // + // Servants should not be saved yet. + // + test(evictor.getLastSavedValue() == -1); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i + 100); + } + + // + // Evict and verify values. + // + evictor.setSize(0); + evictor.setSize(size); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i + 100); + } + + // + // Destroy servants and verify ObjectNotExistException. + // + for(int i = 0; i < size; i++) + { + servants[i].destroy(); + try + { + servants[i].getValue(); + test(false); + } + catch(Ice.ObjectNotExistException ex) + { + // Expected + } + } + + // + // Allocate space for size+1 servants. + // + servants = new Test.ServantPrx[size + 1]; + + // + // Recreate servants. + // + for(int i = 0; i < size; i++) + { + servants[i] = evictor.createServant(i); + } + + // + // Deactivate and recreate evictor, to ensure that servants + // are restored properly after database close and reopen. + // + evictor.deactivate(); + evictor = factory.createEvictor("saveUponEviction", Test.EvictorPersistenceMode.SaveUponEviction); + evictor.setSize(size); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i); + } + + // + // No servants should have been saved yet. + // + test(evictor.getLastSavedValue() == -1); + + // + // Create new servant - should cause eviction. + // + servants[size] = evictor.createServant(size); + test(evictor.getLastSavedValue() == 0); + + // + // Restore the evicted servant, which evicts another + // servant, and so on. + // + for(int i = 0; i <= size; i++) + { + test(servants[i].getValue() == i); + test(evictor.getLastSavedValue() == (i + 1) % (size + 1)); + } + + // + // Destroy new servant and verify eviction no longer occurs. + // + servants[size].destroy(); + evictor.clearLastSavedValue(); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i); + } + test(evictor.getLastSavedValue() == -1); + + // + // Clean up. + // + for(int i = 0; i < size; i++) + { + servants[i].destroy(); + } + + System.out.println("ok"); + } + + // + // Test SaveAfterMutatingOperation mode + // + { + System.out.print("testing SaveAfterMutatingOperation mode... "); + System.out.flush(); + + final int size = 5; + + Test.RemoteEvictorPrx evictor = + factory.createEvictor("saveAfterMutatingOperation", + Test.EvictorPersistenceMode.SaveAfterMutatingOperation); + evictor.setSize(size); + + // + // Create the same number of servants as the evictor size + // (i.e., don't exceed queue size). Servants should be + // saved immediately. + // + Test.ServantPrx[] servants = new Test.ServantPrx[size]; + for(int i = 0; i < size; i++) + { + servants[i] = evictor.createServant(i); + test(evictor.getLastSavedValue() == i); + } + + // + // Evict and verify values. + // + evictor.setSize(0); + evictor.setSize(size); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i); + } + + // + // Mutate servants and verify they have been saved. + // + for(int i = 0; i < size; i++) + { + servants[i].setValue(i + 100); + test(evictor.getLastSavedValue() == i + 100); + } + + // + // Evict and verify values. + // + evictor.setSize(0); + evictor.setSize(size); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i + 100); + } + + // + // No servants should have been saved yet. + // + test(evictor.getLastSavedValue() == -1); + + // + // Destroy servants and verify ObjectNotExistException. + // + for(int i = 0; i < size; i++) + { + servants[i].destroy(); + try + { + servants[i].getValue(); + test(false); + } + catch(Ice.ObjectNotExistException ex) + { + // Expected + } + } + + // + // Allocate space for size+1 servants. + // + servants = new Test.ServantPrx[size + 1]; + + // + // Recreate servants. + // + for(int i = 0; i < size; i++) + { + servants[i] = evictor.createServant(i); + } + + // + // Deactivate and recreate evictor, to ensure that servants + // are restored properly after database close and reopen. + // + evictor.deactivate(); + evictor = factory.createEvictor("saveAfterMutatingOperation", + Test.EvictorPersistenceMode.SaveAfterMutatingOperation); + evictor.setSize(size); + for(int i = 0; i < size; i++) + { + test(servants[i].getValue() == i); + } + + // + // No servants should have been saved yet. + // + test(evictor.getLastSavedValue() == -1); + + // + // Create new servant - should cause eviction but no + // servants should be saved. + // + servants[size] = evictor.createServant(size); + test(evictor.getLastSavedValue() == size); + + // + // Restore the evicted servant, which evicts another + // servant, and so on. + // + for(int i = 0; i <= size; i++) + { + test(servants[i].getValue() == i); + } + test(evictor.getLastSavedValue() == -1); + + // + // Clean up. + // + for(int i = 0; i <= size; i++) + { + servants[i].destroy(); + } + + System.out.println("ok"); + } + + factory.shutdown(); + + return 0; + } + + public static void + main(String[] args) + { + int status = 0; + Ice.Communicator communicator = null; + + try + { + communicator = Ice.Util.initialize(args); + status = run(args, communicator); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + status = 1; + } + + if(communicator != null) + { + try + { + communicator.destroy(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + status = 1; + } + } + + System.exit(status); + } +} diff --git a/java/test/Freeze/evictor/RemoteEvictorFactoryI.java b/java/test/Freeze/evictor/RemoteEvictorFactoryI.java new file mode 100644 index 00000000000..2c9d7434cd2 --- /dev/null +++ b/java/test/Freeze/evictor/RemoteEvictorFactoryI.java @@ -0,0 +1,68 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +public final class RemoteEvictorFactoryI extends Test._RemoteEvictorFactoryDisp +{ + RemoteEvictorFactoryI(Ice.ObjectAdapter adapter, Freeze.DBEnvironment dbEnv) + { + _adapter = adapter; + _dbEnv = dbEnv; + } + + static class Initializer extends Ice.LocalObjectImpl implements Freeze.ServantInitializer + { + public void + initialize(Ice.ObjectAdapter adapter, Ice.Identity ident, Ice.Object servant) + { + ServantI servantImpl = (ServantI)servant; + servantImpl.init(_remoteEvictor, _evictor); + } + + Initializer(RemoteEvictorI remoteEvictor, Freeze.Evictor evictor) + { + _remoteEvictor = remoteEvictor; + _evictor = evictor; + } + + private RemoteEvictorI _remoteEvictor; + private Freeze.Evictor _evictor; + } + + public Test.RemoteEvictorPrx + createEvictor(String name, Test.EvictorPersistenceMode mode, Ice.Current current) + { + Freeze.DB db = _dbEnv.openDB(name, true); + + Freeze.EvictorPersistenceMode fMode; + if(mode == Test.EvictorPersistenceMode.SaveUponEviction) + { + fMode = Freeze.EvictorPersistenceMode.SaveUponEviction; + } + else + { + fMode = Freeze.EvictorPersistenceMode.SaveAfterMutatingOperation; + } + Freeze.Evictor evictor = db.createEvictor(fMode); + _adapter.addServantLocator(evictor, name); + + RemoteEvictorI remoteEvictor = new RemoteEvictorI(_adapter, name, db, evictor); + evictor.installServantInitializer(new Initializer(remoteEvictor, evictor)); + return Test.RemoteEvictorPrxHelper.uncheckedCast(_adapter.add(remoteEvictor, Ice.Util.stringToIdentity(name))); + } + + public void + shutdown(Ice.Current current) + { + _dbEnv.getCommunicator().shutdown(); + } + + private Ice.ObjectAdapter _adapter; + private Freeze.DBEnvironment _dbEnv; +} diff --git a/java/test/Freeze/evictor/RemoteEvictorI.java b/java/test/Freeze/evictor/RemoteEvictorI.java new file mode 100644 index 00000000000..f0118387012 --- /dev/null +++ b/java/test/Freeze/evictor/RemoteEvictorI.java @@ -0,0 +1,71 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +public final class RemoteEvictorI extends Test._RemoteEvictorDisp +{ + RemoteEvictorI(Ice.ObjectAdapter adapter, String category, Freeze.DB db, Freeze.Evictor evictor) + { + _adapter = adapter; + _category = category; + _db = db; + _evictor = evictor; + _lastSavedValue = -1; + } + + public void + setSize(int size, Ice.Current current) + { + _evictor.setSize(size); + } + + public Test.ServantPrx + createServant(int value, Ice.Current current) + { + Ice.Identity id = new Ice.Identity(); + id.category = _category; + id.name = "" + value; + ServantI servant = new ServantI(this, _evictor, value); + _evictor.createObject(id, servant); + return Test.ServantPrxHelper.uncheckedCast(_adapter.createProxy(id)); + } + + public int + getLastSavedValue(Ice.Current current) + { + int result = _lastSavedValue; + _lastSavedValue = -1; + return result; + } + + public void + clearLastSavedValue(Ice.Current current) + { + _lastSavedValue = -1; + } + + public void + deactivate(Ice.Current current) + { + _adapter.removeServantLocator(_category); + _db.close(); + } + + void + setLastSavedValue(int value) + { + _lastSavedValue = value; + } + + private Ice.ObjectAdapter _adapter; + private String _category; + private Freeze.DB _db; + private Freeze.Evictor _evictor; + private int _lastSavedValue; +} diff --git a/java/test/Freeze/evictor/ServantI.java b/java/test/Freeze/evictor/ServantI.java new file mode 100644 index 00000000000..d2ed57bee41 --- /dev/null +++ b/java/test/Freeze/evictor/ServantI.java @@ -0,0 +1,65 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +public final class ServantI extends Test.Servant +{ + ServantI() + { + } + + ServantI(RemoteEvictorI remoteEvictor, Freeze.Evictor evictor, int value) + { + _remoteEvictor = remoteEvictor; + _evictor = evictor; + this.value = value; + } + + void + init(RemoteEvictorI remoteEvictor, Freeze.Evictor evictor) + { + _remoteEvictor = remoteEvictor; + _evictor = evictor; + } + + public void + destroy(Ice.Current current) + { + _evictor.destroyObject(current.id); + } + + public int + getValue(Ice.Current current) + { + return value; + } + + public void + setValue(int value, Ice.Current current) + { + this.value = value; + } + + public void + __write(IceInternal.BasicStream os) + { + _remoteEvictor.setLastSavedValue(value); + super.__write(os); + } + + public void + __marshal(Ice.Stream os) + { + _remoteEvictor.setLastSavedValue(value); + super.__marshal(os); + } + + private RemoteEvictorI _remoteEvictor; + private Freeze.Evictor _evictor; +} diff --git a/java/test/Freeze/evictor/Server.java b/java/test/Freeze/evictor/Server.java new file mode 100644 index 00000000000..96dcaaee3f4 --- /dev/null +++ b/java/test/Freeze/evictor/Server.java @@ -0,0 +1,97 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +public class Server +{ + static class ServantFactory extends Ice.LocalObjectImpl implements Ice.ObjectFactory + { + public Ice.Object + create(String type) + { + assert(type.equals("::Test::Servant")); + return new ServantI(); + } + + public void + destroy() + { + } + } + + static int + run(String[] args, Ice.Communicator communicator, Freeze.DBEnvironment dbEnv) + { + communicator.getProperties().setProperty("Evictor.Endpoints", "default -p 12345 -t 2000"); + + Ice.ObjectAdapter adapter = communicator.createObjectAdapter("Evictor"); + + RemoteEvictorFactoryI factory = new RemoteEvictorFactoryI(adapter, dbEnv); + adapter.add(factory, Ice.Util.stringToIdentity("factory")); + + Ice.ObjectFactory servantFactory = new ServantFactory(); + communicator.addObjectFactory(servantFactory, "::Test::Servant"); + + adapter.activate(); + + communicator.waitForShutdown(); + + return 0; + } + + public static void + main(String[] args) + { + int status = 0; + Ice.Communicator communicator = null; + Freeze.DBEnvironment dbEnv = null; + String dbEnvDir = "db"; + + try + { + Ice.StringSeqHolder holder = new Ice.StringSeqHolder(); + holder.value = args; + communicator = Ice.Util.initialize(holder); + args = holder.value; + if(args.length > 0) + { + dbEnvDir = args[0]; + dbEnvDir += "/"; + dbEnvDir += "db"; + } + dbEnv = Freeze.Util.initialize(communicator, dbEnvDir); + status = run(args, communicator, dbEnv); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + status = 1; + } + + if(dbEnv != null) + { + dbEnv.close(); + } + + if(communicator != null) + { + try + { + communicator.destroy(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + status = 1; + } + } + + System.exit(status); + } +} diff --git a/java/test/Freeze/evictor/Test.ice b/java/test/Freeze/evictor/Test.ice new file mode 100644 index 00000000000..31e4e6c7a0d --- /dev/null +++ b/java/test/Freeze/evictor/Test.ice @@ -0,0 +1,51 @@ +// ********************************************************************** +// +// Copyright (c) 2002 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef TEST_ICE +#define TEST_ICE + +module Test +{ + +class Servant +{ + nonmutating int getValue(); + + void setValue(int value); + + void destroy(); + + int value; +}; + +interface RemoteEvictor +{ + void setSize(int size); + Servant* createServant(int value); + nonmutating int getLastSavedValue(); + void clearLastSavedValue(); + void deactivate(); +}; + +enum EvictorPersistenceMode +{ + SaveUponEviction, + SaveAfterMutatingOperation +}; + +interface RemoteEvictorFactory +{ + RemoteEvictor* createEvictor(string name, EvictorPersistenceMode mode); + void shutdown(); +}; + +}; + +#endif diff --git a/java/test/Freeze/evictor/build.xml b/java/test/Freeze/evictor/build.xml new file mode 100644 index 00000000000..249d5d8c544 --- /dev/null +++ b/java/test/Freeze/evictor/build.xml @@ -0,0 +1,44 @@ +<project name="test_Freeze_evictor" default="all" basedir="."> + + <!-- set global properties for this build --> + <property name="top.dir" value="../../.."/> + <property name="lib.dir" value="${top.dir}/lib"/> + <property name="class.dir" value="classes"/> + <property name="generated.dir" value="generated"/> + <property name="tags.dir" value="${generated.dir}/.tags"/> + + <!-- install slice2java task --> + <taskdef name="slice2java" classpath="${top.dir}/ant" + classname="Slice2JavaTask" /> + + <target name="init"> + <!-- Create the time stamp --> + <tstamp/> + </target> + + <target name="generate" depends="init"> + <!-- Create the output directory for generated code --> + <mkdir dir="${generated.dir}"/> + <mkdir dir="${tags.dir}"/> + <slice2java tagdir="${tags.dir}" outputdir="${generated.dir}"> + <fileset dir="." includes="Test.ice"/> + </slice2java> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir="${generated.dir}" destdir="${class.dir}" + source="1.4" classpath="${lib.dir}" debug="${debug}"/> + <javac srcdir="." destdir="${class.dir}" source="1.4" + classpath="${lib.dir}:${class.dir}" excludes="generated/**" debug="${debug}"/> + </target> + + <target name="all" depends="compile"/> + + <target name="clean"> + <delete dir="${tags.dir}"/> + <delete dir="${generated.dir}"/> + <delete dir="${class.dir}"/> + </target> + +</project> diff --git a/java/test/Freeze/evictor/run.py b/java/test/Freeze/evictor/run.py new file mode 100755 index 00000000000..ff65fac8556 --- /dev/null +++ b/java/test/Freeze/evictor/run.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2002 +# Mutable Realms, Inc. +# Huntsville, AL, USA +# +# All Rights Reserved +# +# ********************************************************************** + +import os, sys + +for toplevel in [".", "..", "../..", "../../..", "../../../.."]: + toplevel = os.path.normpath(toplevel) + if os.path.exists(os.path.join(toplevel, "config", "TestUtil.py")): + break +else: + raise "can't find toplevel directory!" + +sys.path.append(os.path.join(toplevel, "config")) +import TestUtil + +testdir = os.path.join(toplevel,"test", "Freeze", "evictor") + +dbdir = os.path.join(testdir, "db") +TestUtil.cleanDbDir(dbdir) + +TestUtil.clientServerTestWithOptions(toplevel, "Freeze/evictor", " " + testdir, "") +sys.exit(0) |