summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/testing/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java
blob: f03144db8a0b095ee607f444f38aee841cd6aec5 (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.itest;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Assert;
import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Service;

/**
 * This is the client implementation for the call backs in a separate thread tests 
 */
@Service(CallBackSeparateThreadClient.class)
public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadClient, EventProcessorCallBack {
    /**
     * Max time to wait to receive events. If not all the events are received then timeout.
     */
    private static final int TIMEOUT = 30 * 1000;

    /**
     * Counts the number of fast call backs.
     */
    private static final AtomicInteger FAST_CALLBACK_COUNT = new AtomicInteger();

    /**
     * Counts the number of slow call backs.
     */
    private static final AtomicInteger SLOW_CALLBACK_COUNT = new AtomicInteger();

    /**
     * This is our injected reference to the EventProcessorService
     */
    @Reference
    protected EventProcessorService aCallBackService;

    /**
     * This tests call back patterns using separate threads.
     */
    public void runTests() {
    	try {
	        // Register for fast call back
	        registerForFastCallback();
	
	        // Wait for a few fast call backs
	        System.out.println("Waiting for some fast call backs");
	        waitForSomeFastCallbacks();
	
	        try {
		        // Register for slow call back
		        registerForSlowCallback();
		
		        // Wait for a few fast call backs
		        System.out.println("Waiting for some fast calls");
		        waitForSomeFastCallbacks();
		
		        // Wait for a few slow call backs
		        System.out.println("Waiting for some slow calls");
		        waitForSomeSlowCallbacks();
	        } finally {
	        	unregisterForSlowCallback();
	        }
	
	        System.out.println("Done");
    	} finally {
    		unregisterForFastCallback();
    	}
    }

    /**
     * Waits for some fast call backs to be fired
     */
    private void waitForSomeFastCallbacks() {
        // Reset the fast call back count
        FAST_CALLBACK_COUNT.set(0);

        // Wait until we have 10 fast call backs or timeout occurs
        final long start = System.currentTimeMillis();
        do {
            if (FAST_CALLBACK_COUNT.get() >= 10) {
                System.out.println("Received enough fast notifications");
                return;
            }

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                Assert.fail("Unexpeceted exception " + e);
            }
        } while (System.currentTimeMillis() - start < TIMEOUT);

        // If we get to here then we did not receive enough events
        Assert.fail("Did not receive enough fast events");
    }

    /**
     * Waits for some slow call backs to be fired
     */
    private void waitForSomeSlowCallbacks() {
        // Reset the slow call back count
        SLOW_CALLBACK_COUNT.set(0);

        // Wait until we have 4 slow call backs or timeout
        final long start = System.currentTimeMillis();
        do {
            if (SLOW_CALLBACK_COUNT.get() >= 4) {
                System.out.println("Received enough slow notifications");
                return;
            }

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                Assert.fail("Unexpeceted exception " + e);
            }
        } while (System.currentTimeMillis() - start < TIMEOUT);

        // If we get to here then we did not receive enough events
        Assert.fail("Did not receive enough slow events");
    }

    /**
     * Register to receive fast call backs
     */
    private void registerForFastCallback() {
        aCallBackService.registerForEvent("FAST");
    }

    /**
     * Register to receive slow call backs
     */
    private void registerForSlowCallback() {
        aCallBackService.registerForEvent("SLOW");
    }

    /**
     * Unregister to receive fast call backs
     */
    private void unregisterForFastCallback() {
        aCallBackService.unregisterForEvent("FAST");
    }

    /**
     * Unregister to receive slow call backs
     */
    private void unregisterForSlowCallback() {
        aCallBackService.unregisterForEvent("SLOW");
    }

    /**
     * Method that is called when an Event is delivered.
     * 
     * @param aEventName The name of the Event
     * @param aEventData The Event data
     */
    public void eventNotification(String aEventName, Object aEventData) {
        // System.out.println("Received Event : " + aEventName + " " + aEventData);

        if (aEventName.equals("FAST")) {
            final int newValue = FAST_CALLBACK_COUNT.incrementAndGet();
            //System.out.println("Received total of " + newValue + " fast call backs");
        } else if (aEventName.equals("SLOW")) {
            final int newValue = SLOW_CALLBACK_COUNT.incrementAndGet();
            //System.out.println("Received total of " + newValue + " slow call backs");
        } else {
            System.out.println("Unknown event type of " + aEventName);
        }
    }
}