Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9061b39ad828ae00a233346a0793f9c8d5fcdcf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Created on Jan 26, 2010
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.framework.messaging.services.internal;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import junit.framework.Assert;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.ConnectionNode;
import org.eclipse.osee.framework.messaging.NodeInfo;
import org.eclipse.osee.framework.messaging.OseeMessagingListener;
import org.eclipse.osee.framework.messaging.OseeMessagingStatusCallback;
import org.eclipse.osee.framework.messaging.ReplyConnection;
import org.eclipse.osee.framework.messaging.internal.BaseBrokerTesting;
import org.eclipse.osee.framework.messaging.internal.TestMessages;
import org.eclipse.osee.framework.messaging.services.RemoteServiceLookup;
import org.eclipse.osee.framework.messaging.services.RemoteServiceRegistrar;
import org.eclipse.osee.framework.messaging.services.ServiceInfoPopulator;
import org.eclipse.osee.framework.messaging.services.ServiceNotification;
import org.eclipse.osee.framework.messaging.services.messages.ServiceDescriptionPair;
import org.eclipse.osee.framework.messaging.services.messages.ServiceHealth;

/**
 * @author Andrew M. Finkbeiner
 *
 */
public class TestMessageServices extends BaseBrokerTesting {

	private static String BROKER_URI_SERVER = "tcp://localhost:61616";
	private static String BROKER_URI = "tcp://localhost:61616";

	@org.junit.Before
	public void startBroker(){
		try {
			startEmbeddedBroker("testBroker", BROKER_URI_SERVER);
		} catch (Exception ex) {
			ex.printStackTrace();
		}	
	}
	
	@org.junit.After
	public void stopBroker(){
		try {
			stopEmbeddedBroker("testBroker", BROKER_URI_SERVER);
		} catch (Exception ex) {
			ex.printStackTrace();
		}	
	}
	
//	@Ignore
	@org.junit.Test
	public void testServiceUpClientComesUp() throws Exception{
		ConnectionNode connection = getMessaging().get(new NodeInfo("osee-jms", new URI(BROKER_URI)));
		Assert.assertNotNull(connection);
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
		
		RemoteServiceRegistrar registrar = new RemoteServiceRegistrarImpl(connection, executor);
		RemoteServiceLookup lookup = new RemoteServiceLookupImpl(connection, executor);
		registrar.start();
		lookup.start();
		
		registrar.registerService("testService", "1002", "some.service.id", new URI("tcp://localhost:666"), new TestPopulator(), 30);
		
		TestNotification testNotification = new TestNotification();
		lookup.register("testService", "1002", testNotification);
		
		testWait(3000);
		Assert.assertTrue(testNotification.getServiceUpdates() >= 1);
		int currentNumberOfUpdates = testNotification.getServiceUpdates();
		testWait(35000);
		
		Assert.assertEquals(currentNumberOfUpdates + 1, testNotification.getServiceUpdates());
		Assert.assertTrue(registrar.unregisterService("testService", "1002", "some.service.id"));
		
		testWait(1000);
		
		Assert.assertEquals(1, testNotification.getServiceAway());//message from unregister
		
		testWait(90000);
		
		Assert.assertEquals(2, testNotification.getServiceAway());//service renewal timout
		Assert.assertEquals(currentNumberOfUpdates + 1, testNotification.getServiceUpdates());
		Assert.assertTrue(lookup.unregister("testService", "1002", testNotification));
		
		registrar.stop();
		lookup.stop();
		testWait(2000);
	}
	
//	@Ignore
	@org.junit.Test
	public void testClientUpServiceComesUp() throws Exception{
		ConnectionNode connection = getMessaging().get(new NodeInfo("osee-jms", new URI(BROKER_URI)));
		Assert.assertNotNull(connection);
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
		RemoteServiceRegistrar registrar = new RemoteServiceRegistrarImpl(connection, executor);
		registrar.start();
		
		RemoteServiceLookup lookup = new RemoteServiceLookupImpl(connection, executor);
		lookup.start();
		
		TestNotification testNotification = new TestNotification();
		lookup.register("testService", "1002", testNotification);
		
		registrar.registerService("testService", "1002", "some.service.id", new URI("tcp://localhost:666"), new TestPopulator(), 30);
		testWait(3000);
		
		Assert.assertTrue(testNotification.getServiceUpdates() >= 1);
		int currentNumberOfUpdates = testNotification.getServiceUpdates();
		
		testWait(35000);
		
		Assert.assertEquals(currentNumberOfUpdates + 1, testNotification.getServiceUpdates());
		Assert.assertTrue(registrar.unregisterService("testService", "1002", "some.service.id"));
		
		testWait(1000);
      
      Assert.assertEquals(1, testNotification.getServiceAway());//message from unregister
      
      testWait(90000);
      
      Assert.assertEquals(2, testNotification.getServiceAway());//service renewal timout
		
		Assert.assertEquals(currentNumberOfUpdates + 1, testNotification.getServiceUpdates());
		Assert.assertTrue(lookup.unregister("testService", "1002", testNotification));
		
		lookup.stop();
		registrar.stop();
		testWait(2000);
	}
	
//	@Ignore
	@org.junit.Test
	public void testServiceComesUpClientGetsReply() throws Exception{
		ConnectionNode connection = getMessaging().get(new NodeInfo("osee-jms", new URI(BROKER_URI)));
		Assert.assertNotNull(connection);
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
		
		RemoteServiceRegistrar registrar = new RemoteServiceRegistrarImpl(connection, executor);
		registrar.start();
		registrar.registerService("testService", "1002", "some.service.id", new URI("tcp://localhost:666"), new TestPopulator(), 50000);
		
		
		testWait(2000);
		RemoteServiceLookup lookup = new RemoteServiceLookupImpl(connection, executor);
		lookup.start();
		TestNotification testNotification = new TestNotification();
		lookup.register("testService", "1002", testNotification);
		testWait(1000);
		
		Assert.assertEquals(1, testNotification.getServiceUpdates());
		
		Assert.assertTrue(lookup.unregister("testService", "1002", testNotification));
		Assert.assertTrue(registrar.unregisterService("testService", "1002", "some.service.id"));
		
		registrar.stop();
		lookup.stop();
		testWait(2000);
	}
	
//	@Ignore
	@org.junit.Test
	public void testReply() throws Exception{
		ConnectionNode connection = getMessaging().get(new NodeInfo("osee-jms", new URI(BROKER_URI)));
		TestReplyListener service = new TestReplyListener();
		TestReplyListener replyReciever = new TestReplyListener();
		connection.subscribe(TestMessages.replyTopic, service, new OseeMessagingStatusImpl("failed to subscribe", TestMessageServices.class));
		connection.subscribeToReply(TestMessages.replyTopic, replyReciever);
		testWait(1000);
		System.out.println(System.currentTimeMillis());
		connection.send(TestMessages.replyTopic, "test", new BasicOseeMessagingStatus());
		System.out.println(System.currentTimeMillis());
		testWait(5000);
		System.out.println(System.currentTimeMillis());
		Assert.assertEquals(1, service.sentReply);
		Assert.assertEquals(1, replyReciever.receivedReply);
		connection.unsubscribe(TestMessages.replyTopic, service, new OseeMessagingStatusImpl("failed to subscribe", TestMessageServices.class));
	}
	
	private class TestReplyListener extends OseeMessagingListener {
		int sentReply;
		int receivedReply;
		String msg = "back at ya";
		@Override
		public void process(Object message, Map<String, Object> headers,
				ReplyConnection replyConnection) {
			if(replyConnection.isReplyRequested()){
				try {
					System.out.println(System.currentTimeMillis());
					System.out.println(System.currentTimeMillis() +" reply requested - " + msg);
					replyConnection.send(msg, null, new BasicOseeMessagingStatus());
					sentReply++;
				} catch (OseeCoreException ex) {
					ex.printStackTrace();
				}
			} else {
				System.out.println(System.currentTimeMillis() +" got a reply - " + message);
				if(message.toString().equals(msg)){
					receivedReply++;
				}
			}
		}
	}
	
	private class BasicOseeMessagingStatus implements OseeMessagingStatusCallback	{
		@Override
		public void fail(Throwable th) {
			th.printStackTrace();
			OseeLog.log(BasicOseeMessagingStatus.class, Level.SEVERE, th);
		}
		@Override
		public void success() {
		}
	}
	
	private class TestPopulator implements ServiceInfoPopulator {

		@Override
		public void updateServiceInfo(
				List<ServiceDescriptionPair> serviceDescription) {
			ServiceDescriptionPair pair1 = new ServiceDescriptionPair();
			pair1.setName("test");
			pair1.setValue("one");
			serviceDescription.add(pair1);
		}
		
	}
	
	private class TestNotification implements ServiceNotification {

		private int serviceUpdates = 0;
		private int serviceAway = 0;

		public int getServiceUpdates() {
			return serviceUpdates;
		}
		public int getServiceAway(){
			return serviceAway;
		}

		@Override
		public void onServiceGone(ServiceHealth serviceHealth) {
			serviceAway++;
			System.out.println("serviceAway " + serviceAway);
		}

		@Override
		public void onServiceUpdate(ServiceHealth serviceHealth) {
			serviceUpdates++;
			System.out.println("healthUpdates " + serviceUpdates);
		}
		
	}
}

Back to the top