01: /*
02: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
19: * The latest copy of this software may be found on http://www.xsocket.org/
20: */
21: package org.xsocket.connection;
22:
23: import java.io.IOException;
24: import java.nio.BufferUnderflowException;
25: import java.util.ArrayList;
26: import java.util.List;
27:
28: import org.junit.Assert;
29: import org.junit.Test;
30: import org.xsocket.QAUtil;
31: import org.xsocket.connection.IDataHandler;
32: import org.xsocket.connection.INonBlockingConnection;
33: import org.xsocket.connection.IServer;
34: import org.xsocket.connection.NonBlockingConnection;
35: import org.xsocket.connection.Server;
36: import org.xsocket.connection.ConnectionUtils;
37:
38: /**
39: *
40: * @author grro@xsocket.org
41: */
42: public final class ConcurrentCallbackCallsTest {
43:
44: @Test
45: public void testSimple() throws Exception {
46:
47: TestHandler hdl = new TestHandler();
48: final IServer server = new Server(hdl);
49: ConnectionUtils.start(server);
50:
51: INonBlockingConnection connection = new NonBlockingConnection(
52: "localhost", server.getLocalPort());
53:
54: for (int i = 0; i < 20; i++) {
55: connection.write((int) 4);
56: }
57: QAUtil.sleep(300);
58:
59: connection.close();
60: server.close();
61:
62: Assert.assertTrue(hdl.errors.isEmpty());
63: }
64:
65: private static class TestHandler implements IDataHandler {
66:
67: private int concurrent = 0;
68: private List<String> errors = new ArrayList<String>();
69:
70: public boolean onData(INonBlockingConnection connection)
71: throws IOException, BufferUnderflowException {
72: try {
73: concurrent++;
74:
75: if (concurrent != 1) {
76: errors.add(concurrent + " concurrent calls");
77: }
78: int i = connection.readInt();
79: connection.write(i);
80: QAUtil.sleep(100);
81:
82: } finally {
83: concurrent--;
84: }
85:
86: return true;
87: }
88: }
89: }
|