summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1-RC1/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java
blob: e861f8a3d17fc8d50b6cd6becbeb2609021a39fe (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
/*
 * 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 junit.framework.Assert;

import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Service;

/**
 * This is the client implementation for the call backs in a separate thread tests 
 */
@Service(CallBackSeparateThreadClient.class)
public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadClient, EventProcessorCallBack {
    /**
     * Used to sleep for 60 seconds.
     */
    private static final int SIXTY_SECONDS = 60 * 1000;

    /**
     * Counts the number of one second call backs
     */
    private static final AtomicInteger oneSecondCallbackCount = new AtomicInteger();

    /**
     * Counts the number of five second call backs
     */
    private static final AtomicInteger fiveSecondCallbackCount = 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() {
        // Register for 1 second call back
        registerFor1SecondCallback();
        
        // Wait for a few 1 second call backs
        System.out.println("Waiting for some 1 second calls");
        waitForSome1SecondCallbacks();

        // Register for 5 second call back
        registerFor5SecondCallback();
        
        // Wait for a few 1 second call backs
        System.out.println("Waiting for some 1 second calls");
        waitForSome1SecondCallbacks();
        
        // Wait for a few 5 second call backs
        System.out.println("Waiting for some 5 second calls");
        waitForSome5SecondCallbacks();
        
        System.out.println("Done");
    }

    /**
     * Waits for some one second call backs to be fired
     */
    private void waitForSome1SecondCallbacks() {
        // Reset the one second call back count
        oneSecondCallbackCount.set(0);
        
        // Wait until we have 10 1 second call backs or 60 seconds has passed
        final long start = System.currentTimeMillis();
        do {
            if (oneSecondCallbackCount.get() >= 10) {
                System.out.println("Received enough 1 second notifications");
                return;
            }
            
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Assert.fail("Unexpeceted exception " + e);
            }
        }
        while (System.currentTimeMillis() - start < SIXTY_SECONDS);
        
        // If we get to here then we did not receive enough events
        Assert.fail("Did not receive enough 1 second events");
    }

    /**
     * Waits for some five second call backs to be fired
     */
    private void waitForSome5SecondCallbacks() {
        // Reset the five second call back count
        fiveSecondCallbackCount.set(0);
        
        // Wait until we have 4 5 second call backs or 60 seconds has passed
        final long start = System.currentTimeMillis();
        do
        {
            if (fiveSecondCallbackCount.get() >= 4) {
                System.out.println("Received enough 5 second notifications");
                return;
            }
            
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException e)
            {
                Assert.fail("Unexpeceted exception " + e);
            }
        }
        while (System.currentTimeMillis() - start < SIXTY_SECONDS);
        
        // If we get to here then we did not receive enough events
        Assert.fail("Did not receive enough 5 second events");
    }
    
    /**
     * Register to receive one second call backs
     */
    private void registerFor1SecondCallback() {
        aCallBackService.registerForEvent("ONE");
        return;
    }

    /**
     * Register to receive five second call backs
     */
    private void registerFor5SecondCallback() {
        aCallBackService.registerForEvent("FIVE");
    }

    /**
     * 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("ONE")) {
            final int newValue = oneSecondCallbackCount.incrementAndGet();
            //System.out.println("Received total of " + newValue + " 1 second call backs");
        } else if (aEventName.equals("FIVE")) {
            final int newValue = fiveSecondCallbackCount.incrementAndGet();
            //System.out.println("Received total of " + newValue + " 5 second call backs");
        }
        else
            System.out.println("Unknown event type of " + aEventName);
    }
}