KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > storage > TCServerStorageManager


1 /*
2  * Created on May 24, 2005
3  *
4   */

5 package org.apache.sandesha.storage;
6
7 import junit.framework.TestCase;
8 import org.apache.sandesha.Constants;
9 import org.apache.sandesha.RMMessageContext;
10 import org.apache.sandesha.server.ServerStorageManager;
11 import org.apache.sandesha.storage.queue.QueueException;
12 import org.apache.sandesha.storage.queue.SandeshaQueue;
13
14
15 public class TCServerStorageManager extends TestCase {
16
17     //For testing weather messages are re-transmitted correctly
18
public void testRetransmission() {
19
20         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
21         ServerStorageManager csm = new ServerStorageManager();
22         RMMessageContext msg1;
23
24 //approving the out sequence
25
csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
26         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
27
28 //messages should be returned (since the out sequence is approved)
29
msg1 = csm.getNextMessageToSend();
30         assertNotNull(msg1);
31         msg1.setLocked(false);
32         msg1 = csm.getNextMessageToSend();
33         assertNotNull(msg1);
34         msg1.setLocked(false);
35         msg1 = csm.getNextMessageToSend();
36         assertNotNull(msg1);
37         msg1.setLocked(false);
38         msg1 = csm.getNextMessageToSend();
39         assertNotNull(msg1);
40         msg1.setLocked(false);
41         msg1 = csm.getNextMessageToSend();
42         assertNull(msg1);
43
44 //Waiting for little more than re-transmission interval
45
try {
46             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
47         } catch (InterruptedException JavaDoc e) {
48             e.printStackTrace();
49         }
50
51
52         msg1 = csm.getNextMessageToSend();
53         assertNotNull(msg1);
54         msg1.setLocked(false);
55         msg1 = csm.getNextMessageToSend();
56         assertNotNull(msg1);
57         msg1.setLocked(false);
58         msg1 = csm.getNextMessageToSend();
59         assertNotNull(msg1);
60         msg1.setLocked(false);
61         msg1 = csm.getNextMessageToSend();
62         assertNotNull(msg1);
63         msg1.setLocked(false);
64         msg1 = csm.getNextMessageToSend();
65         assertNull(msg1);
66
67 //Again waiting for little more than re-transmission interval :)
68
try {
69             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
70         } catch (InterruptedException JavaDoc e) {
71             e.printStackTrace();
72         }
73
74         //Messages should be returned once again
75
msg1 = csm.getNextMessageToSend();
76         assertNotNull(msg1);
77         msg1.setLocked(false);
78         msg1 = csm.getNextMessageToSend();
79         assertNotNull(msg1);
80         msg1.setLocked(false);
81         msg1 = csm.getNextMessageToSend();
82         assertNotNull(msg1);
83         msg1.setLocked(false);
84         msg1 = csm.getNextMessageToSend();
85         assertNotNull(msg1);
86         msg1.setLocked(false);
87         msg1 = csm.getNextMessageToSend();
88         assertNull(msg1);
89
90     }
91
92
93     //Testing weather the tr-transmission stops after a acknowledgement
94
public void testAcknowledgement() {
95         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
96         ServerStorageManager csm = new ServerStorageManager();
97
98         csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
99         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
100
101         RMMessageContext msg1;
102         msg1 = csm.getNextMessageToSend();
103         assertNotNull(msg1);
104         msg1.setLocked(false);
105         msg1 = csm.getNextMessageToSend();
106         assertNotNull(msg1);
107         msg1.setLocked(false);
108         msg1 = csm.getNextMessageToSend();
109         assertNotNull(msg1);
110         msg1.setLocked(false);
111         msg1 = csm.getNextMessageToSend();
112         assertNotNull(msg1);
113         msg1.setLocked(false);
114         msg1 = csm.getNextMessageToSend();
115         assertNull(msg1);
116         
117         //Acknowledging messages 1,2 and 4 (without 3)
118
csm.setAcknowledged("approved1", 1);
119         csm.setAcknowledged("approved1", 2);
120         csm.setAcknowledged("approved1", 4);
121         
122         //Waiting for little more than re-transmission interval
123
try {
124             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
125         } catch (InterruptedException JavaDoc e) {
126             e.printStackTrace();
127         }
128
129         //Only message no. 3 should be re-transmitted
130
msg1 = csm.getNextMessageToSend();
131         assertNotNull(msg1);
132         msg1.setLocked(false);
133         assertEquals(msg1.getMessageID(), "rmsg3");
134         msg1 = csm.getNextMessageToSend();
135         assertNull(msg1);
136     }
137
138     //For testing weather getNextMsgToSend method works correctly
139
public void testNextMsgToSend() {
140         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
141         ServerStorageManager csm = new ServerStorageManager();
142         RMMessageContext msg1;
143         
144         //Next message to sent should be null (before approving outsequence)
145
msg1 = csm.getNextMessageToSend();
146         assertNull(msg1);
147         msg1 = csm.getNextMessageToSend();
148         assertNull(msg1);
149         
150         //approving the out sequence
151
csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
152         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
153
154         //messages should be returned (since the out sequence is approved)
155
msg1 = csm.getNextMessageToSend();
156         assertNotNull(msg1);
157         msg1 = csm.getNextMessageToSend();
158         assertNotNull(msg1);
159         msg1 = csm.getNextMessageToSend();
160         assertNotNull(msg1);
161         msg1 = csm.getNextMessageToSend();
162         assertNotNull(msg1);
163         msg1 = csm.getNextMessageToSend();
164         assertNull(msg1);
165     }
166
167     //Testing weather the out-sequence concept works correctly.
168
//Outgoing messages should be sent only when the out sequence is approved.
169
public void testOutSequence() {
170         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
171         ServerStorageManager csm = new ServerStorageManager();
172         
173         //setting temporary out sequence
174
csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
175
176         RMMessageContext msg1;
177         
178         //the message should be null since the out sequence has not been approved
179
msg1 = csm.getNextMessageToSend();
180         assertNull(msg1);
181         
182         //still the message should be null since the out sequence has not been approved
183
msg1 = csm.getNextMessageToSend();
184         assertNull(msg1);
185         
186         //approving the out sequence
187
csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
188
189         //not the message should not be null (since out sequence was approved)
190
msg1 = csm.getNextMessageToSend();
191         assertNotNull(msg1);
192     }
193
194     //Testing weather priority messages are sent correctly.
195
//They should be sent before application messages and should be sent
196
//even before the out-sequence get set.
197
public void testPriorityQueue() {
198         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
199         ServerStorageManager csm = new ServerStorageManager();
200         
201         //Addign a create sequence request message (this will be added to the priority
202
//area of the storage)
203
RMMessageContext createSeqReq1 = new RMMessageContext();
204         createSeqReq1.setMessageType(Constants.MSG_TYPE_CREATE_SEQUENCE_REQUEST);
205         createSeqReq1.setMessageID("temp1");
206
207         csm.addCreateSequenceRequest(createSeqReq1);
208
209         RMMessageContext msg1;
210         ServerStorageManager csm1 = new ServerStorageManager();
211
212         //create sequence message should be returned
213
msg1 = csm1.getNextMessageToSend();
214         assertNotNull(msg1);
215         
216         //other messages should not be returned since the out sequence has not been approved.
217
msg1 = csm1.getNextMessageToSend();
218         assertNull(msg1);
219
220         //Addign a acknowledgement message (this will be added to the priority
221
//area of the storage)
222
RMMessageContext ack1 = new RMMessageContext();
223         ack1.setMessageID("msgack1");
224         csm.addAcknowledgement(ack1);
225
226         //ack message should be returned
227
msg1 = csm1.getNextMessageToSend();
228         assertNotNull(msg1);
229         
230         //other messages should not be returned since the out sequence has not been approved.
231
msg1 = csm1.getNextMessageToSend();
232         assertNull(msg1);
233     }
234
235
236     public void setUp() throws QueueException {
237
238         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.SERVER);
239         ServerStorageManager csm = new ServerStorageManager();
240         RMMessageContext msg = new RMMessageContext();
241         
242         //Creating a new outgoing sequence.
243
sq.createNewOutgoingSequence("seqid1");
244         
245         //Adding messages to the outgoing sequence.
246

247         //Adding message 1
248
long nextMsgNo = csm.getNextMessageNumber("seqid1");
249         assertEquals(nextMsgNo, 1);
250         msg.setMessageID("rmsg1");
251         msg.setSequenceID("seqid1");
252         msg.setMsgNumber(nextMsgNo);
253         sq.addMessageToOutgoingSequence("seqid1", msg);
254
255         //Adding message 2
256
nextMsgNo = csm.getNextMessageNumber("seqid1");
257         assertEquals(nextMsgNo, 2);
258         msg = new RMMessageContext();
259         msg.setMessageID("rmsg2");
260         msg.setSequenceID("seqid1");
261         msg.setMsgNumber(nextMsgNo);
262         sq.addMessageToOutgoingSequence("seqid1", msg);
263
264         //Adding message 3
265
nextMsgNo = csm.getNextMessageNumber("seqid1");
266         assertEquals(nextMsgNo, 3);
267         msg = new RMMessageContext();
268         msg.setMessageID("rmsg3");
269         msg.setSequenceID("seqid1");
270         msg.setMsgNumber(nextMsgNo);
271
272         //Adding message 4
273
sq.addMessageToOutgoingSequence("seqid1", msg);
274         nextMsgNo = csm.getNextMessageNumber("seqid1");
275         assertEquals(nextMsgNo, 4);
276         msg = new RMMessageContext();
277         msg.setMessageID("rmsg4");
278         msg.setSequenceID("seqid1");
279         msg.setMsgNumber(nextMsgNo);
280         sq.addMessageToOutgoingSequence("seqid1", msg);
281     }
282
283     public void tearDown() {
284
285         //clearing the storage
286
ServerStorageManager csm = new ServerStorageManager();
287         csm.clearStorage();
288
289     }
290 }
291
Popular Tags