01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.handler;
06:
07: import com.tc.async.impl.MockStage;
08: import com.tc.exception.ImplementMe;
09: import com.tc.net.groups.NodeID;
10: import com.tc.net.protocol.tcm.MessageChannel;
11: import com.tc.object.ObjectID;
12: import com.tc.object.TestRequestManagedObjectMessage;
13: import com.tc.object.net.ChannelStats;
14: import com.tc.objectserver.core.api.ServerConfigurationContext;
15: import com.tc.objectserver.core.impl.TestServerConfigurationContext;
16: import com.tc.objectserver.impl.TestObjectRequestManager;
17: import com.tc.objectserver.l1.api.TestClientStateManager;
18: import com.tc.stats.counter.Counter;
19: import com.tc.stats.counter.CounterImpl;
20:
21: import java.util.HashSet;
22: import java.util.Set;
23:
24: import junit.framework.TestCase;
25:
26: public class ManagedObjectRequestHandlerTest extends TestCase {
27:
28: public void testObjectRequestCounter() {
29: TestChannelStats channelStats = new TestChannelStats();
30: Counter channelReqCounter = new CounterImpl(666L);
31: Counter channelRemCounter = new CounterImpl(69L);
32: Counter requestCounter = new CounterImpl(0L);
33: Counter removeCounter = new CounterImpl(0L);
34: channelStats.reqCounter = channelReqCounter;
35: channelStats.remCounter = channelRemCounter;
36:
37: TestServerConfigurationContext context = new TestServerConfigurationContext();
38: context.clientStateManager = new TestClientStateManager();
39: context
40: .addStage(
41: ServerConfigurationContext.RESPOND_TO_OBJECT_REQUEST_STAGE,
42: new MockStage("yo"));
43: context.channelStats = channelStats;
44:
45: ManagedObjectRequestHandler handler = new ManagedObjectRequestHandler(
46: requestCounter, removeCounter,
47: new TestObjectRequestManager());
48: handler.initializeContext(context);
49:
50: TestRequestManagedObjectMessage msg = new TestRequestManagedObjectMessage();
51: HashSet s = new HashSet();
52: s.add(new ObjectID(1));
53: msg.setObjectIDs(s);
54: Set removed = makeRemovedSet(31);
55: msg.setRemoved(removed);
56:
57: assertEquals(0, requestCounter.getValue());
58: assertEquals(0, removeCounter.getValue());
59: assertEquals(666, channelReqCounter.getValue());
60: assertEquals(69, channelRemCounter.getValue());
61: handler.handleEvent(msg);
62: assertEquals(1, requestCounter.getValue());
63: assertEquals(31, removeCounter.getValue());
64: assertEquals(667, channelReqCounter.getValue());
65: assertEquals(100, channelRemCounter.getValue());
66: }
67:
68: private Set makeRemovedSet(int num) {
69: Set rv = new HashSet();
70: for (int i = 0; i < num; i++) {
71: rv.add(new ObjectID(i));
72: }
73: return rv;
74: }
75:
76: private static class TestChannelStats implements ChannelStats {
77: Counter remCounter;
78: Counter reqCounter;
79:
80: public Counter getCounter(MessageChannel channel, String name) {
81: if (OBJECT_FLUSH_RATE.equals(name)) {
82: return remCounter;
83: } else if (OBJECT_REQUEST_RATE.equals(name)) {
84: return reqCounter;
85: }
86: throw new RuntimeException(name);
87: }
88:
89: public void notifyTransaction(NodeID nodeID) {
90: throw new ImplementMe();
91:
92: }
93: }
94:
95: }
|