summaryrefslogtreecommitdiff
path: root/cppe/test/IceE/faultTolerance/AllTests.cpp
blob: 5fdcd770d1c828ad2866a5c5bda3238a95582f5e (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
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceE/IceE.h>
#include <TestCommon.h>
#include <Test.h>

using namespace std;
using namespace Test;

class CallbackBase : public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    CallbackBase() :
	_called(false)
    {
    }

    virtual ~CallbackBase()
    {
    }

    bool check()
    {
	IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
	while(!_called)
	{
	    if(!timedWait(IceUtil::Time::seconds(30)))
	    {
		return false;
	    }
	}
	_called = false;
	return true;
    }

protected:

    void called()
    {
	IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
	assert(!_called);
	_called = true;
	notify();
    }

private:

    bool _called;
};

void
allTests(const Ice::CommunicatorPtr& communicator, const vector<int>& ports)
{
    tprintf("testing stringToProxy...");
    string ref("test");

    char buf[32];
    for(vector<int>::const_iterator p = ports.begin(); p != ports.end(); ++p)
    {
        sprintf(buf, ":default -t 60000 -p %d", *p);
	ref += buf;
    }
    Ice::ObjectPrx base = communicator->stringToProxy(ref);
    test(base);
    tprintf("ok\n");

    tprintf("testing checked cast...");
    TestIntfPrx obj = TestIntfPrx::checkedCast(base);
    test(obj);
    test(obj == base);
    tprintf("ok\n");

    int oldPid = 0;
    for(unsigned int i = 1, j = 0; i <= ports.size(); ++i, ++j)
    {
	if(j > 3)
	{
	    j = 0;
	}

	tprintf("testing server #%d...", i);
	int pid = obj->pid();
	test(pid != oldPid);
	tprintf("ok\n");
	oldPid = pid;

	if(j == 0)
	{
	    tprintf("shutting down server #%d...", i);
	    obj->shutdown();
	    tprintf("ok\n");
	}
	else if(j == 1 || i + 1 > ports.size())
	{
	    tprintf("aborting server #%d...", i);
	    try
	    {
	        obj->abort();
	        test(false);
	    }
	    catch(const Ice::ConnectionLostException&)
	    {
	        tprintf("ok\n");
	    }
	    catch(const Ice::ConnectFailedException&)
	    {
	        tprintf("ok\n");
	    }
	}
	else if(j == 2)
	{
	    tprintf("aborting server #%d and #%d with idempotent call...", i, i + 1);
	    try
	    {
	        obj->idempotentAbort();
	        test(false);
	    }
	    catch(const Ice::ConnectionLostException&)
	    {
	        tprintf("ok\n");
	    }
	    catch(const Ice::ConnectFailedException&)
	    {
	        tprintf("ok\n");
	    }

	    ++i;
	}
	else if(j == 3)
	{
	    tprintf("aborting server #%d and #%d with nonmutating call...", i, i + 1);
	    try
	    {
	        obj->nonmutatingAbort();
	        test(false);
	    }
	    catch(const Ice::ConnectionLostException&)
	    {
	        tprintf("ok\n");
	    }
	    catch(const Ice::ConnectFailedException&)
	    {
	        tprintf("ok\n");
	    }

	    ++i;
	}
	else
	{
	    assert(false);
	}
    }

    tprintf("testing whether all servers are gone...");
    try
    {
	obj->ice_ping();
	test(false);
    }
    catch(const Ice::LocalException&)
    {
	tprintf("ok\n");
    }
}