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.foundation;
022:
023: import com.db4o.foundation.*;
024:
025: import db4ounit.*;
026:
027: public class BlockingQueueTestCase extends Queue4TestCaseBase {
028: public void testIterator() {
029: Queue4 queue = new BlockingQueue();
030: String[] data = { "a", "b", "c", "d" };
031: for (int idx = 0; idx < data.length; idx++) {
032: assertIterator(queue, data, idx);
033: queue.add(data[idx]);
034: assertIterator(queue, data, idx + 1);
035: }
036: }
037:
038: public void testNext() {
039: Queue4 queue = new BlockingQueue();
040: String[] data = { "a", "b", "c", "d" };
041: queue.add(data[0]);
042: Assert.areSame(data[0], queue.next());
043: queue.add(data[1]);
044: queue.add(data[2]);
045: Assert.areSame(data[1], queue.next());
046: Assert.areSame(data[2], queue.next());
047: }
048:
049: public void testBlocking() {
050: Queue4 queue = new BlockingQueue();
051: String[] data = { "a", "b", "c", "d" };
052: queue.add(data[0]);
053: Assert.areSame(data[0], queue.next());
054:
055: NotifyThread notifyThread = new NotifyThread(queue, data[1]);
056: notifyThread.start();
057: long start = System.currentTimeMillis();
058: Assert.areSame(data[1], queue.next());
059: long end = System.currentTimeMillis();
060: Assert.isGreater(500, end - start);
061: }
062:
063: public void testStop() {
064: final BlockingQueue queue = new BlockingQueue();
065: String[] data = { "a", "b", "c", "d" };
066: queue.add(data[0]);
067: Assert.areSame(data[0], queue.next());
068:
069: StopThread notifyThread = new StopThread(queue);
070: notifyThread.start();
071: Assert.expect(BlockingQueueStoppedException.class,
072: new CodeBlock() {
073: public void run() throws Throwable {
074: queue.next();
075: }
076: });
077: }
078:
079: private static class NotifyThread extends Thread {
080: private Queue4 _queue;
081:
082: private Object _data;
083:
084: NotifyThread(Queue4 queue, Object data) {
085: _queue = queue;
086: _data = data;
087: }
088:
089: public void run() {
090: try {
091: Thread.sleep(1000);
092: } catch (Exception e) {
093:
094: }
095: _queue.add(_data);
096: }
097: }
098:
099: private static class StopThread extends Thread {
100: private BlockingQueue _queue;
101:
102: StopThread(BlockingQueue queue) {
103: _queue = queue;
104: }
105:
106: public void run() {
107: try {
108: Thread.sleep(1000);
109: } catch (Exception e) {
110:
111: }
112: _queue.stop();
113: }
114: }
115:
116: }
|