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.concurrency;
022:
023: import com.db4o.*;
024: import com.db4o.events.*;
025: import com.db4o.ext.*;
026: import com.db4o.foundation.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class CommittedCallbackRefreshTestCase extends
032: Db4oClientServerTestCase {
033:
034: private final Object _lock = new Object();
035:
036: private final int COUNT = 1;
037:
038: public static class Item {
039:
040: public String _name;
041:
042: public SubItem _subItem;
043:
044: public int _updates;
045:
046: public Item(String name, SubItem subItem) {
047: _name = name;
048: _subItem = subItem;
049: }
050:
051: public void update() {
052: _updates++;
053: _subItem._updates++;
054: }
055:
056: public void check() {
057: Assert.isNotNull(_name);
058: Assert.areEqual(_name, _subItem._name);
059: Assert.areEqual(_updates, _subItem._updates);
060: System.out.println(_updates);
061: }
062:
063: }
064:
065: public static class SubItem {
066:
067: public String _name;
068:
069: public int _updates;
070:
071: public SubItem(String name) {
072: _name = name;
073: }
074:
075: }
076:
077: public static void main(String[] arguments) {
078: new CommittedCallbackRefreshTestCase().runConcurrency();
079: }
080:
081: protected void store() {
082: for (int i = 0; i < COUNT; i++) {
083: String name = "original" + i;
084: store(new Item(name, new SubItem(name)));
085: }
086: }
087:
088: public void conc(final ExtObjectContainer oc, int seq) {
089:
090: eventRegistry(oc).committed().addListener(new EventListener4() {
091: public void onEvent(Event4 e, EventArgs args) {
092: if (oc.isClosed()) {
093: return;
094: }
095: ObjectInfoCollection updated = ((CommitEventArgs) args)
096: .updated();
097: Iterator4 infos = updated.iterator();
098: while (infos.moveNext()) {
099: ObjectInfo info = (ObjectInfo) infos.current();
100: Object obj = info.getObject();
101: oc.refresh(obj, 2);
102: }
103: }
104: });
105:
106: Item[] items = new Item[COUNT];
107: ObjectSet objectSet = newQuery(Item.class).execute();
108: int count = 0;
109: while (objectSet.hasNext()) {
110: synchronized (_lock) {
111: items[count] = (Item) objectSet.next();
112: items[count].check();
113: count++;
114: }
115: }
116:
117: for (int i = 0; i < items.length; i++) {
118: synchronized (_lock) {
119: items[i].update();
120: store(items[i]._subItem);
121: store(items[i]);
122: }
123: db().commit();
124:
125: }
126:
127: Cool.sleepIgnoringInterruption(1000);
128:
129: for (int i = 0; i < items.length; i++) {
130: synchronized (_lock) {
131: items[i].check();
132: }
133: }
134:
135: Cool.sleepIgnoringInterruption(3000);
136: }
137:
138: private EventRegistry eventRegistry(final ExtObjectContainer oc) {
139: return EventRegistryFactory.forObjectContainer(oc);
140: }
141: }
|