1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include <IceUtil/IceUtil.h>
6 #include <Ice/Ice.h>
7 #include <Glacier2/Glacier2.h>
8 
9 #include <TestHelper.h>
10 
11 #include <iostream>
12 #include <iomanip>
13 #include <list>
14 
15 #include <Callback.h>
16 
17 using namespace std;
18 using namespace Test;
19 
20 namespace
21 {
22 
23 class CallbackReceiverI : public Test::CallbackReceiver
24 {
25 public:
26 
CallbackReceiverI()27     CallbackReceiverI() : _received(false)
28     {
29     }
30 
callback(const Ice::Current &)31     virtual void callback(const Ice::Current&)
32     {
33         IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
34         _received = true;
35         _monitor.notify();
36     }
37 
waitForCallback()38     void waitForCallback()
39     {
40         IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
41         while(!_received)
42         {
43             _monitor.wait();
44         }
45         _received = false;
46     }
47 
48     IceUtil::Monitor<IceUtil::Mutex> _monitor;
49     bool _received;
50 };
51 ICE_DEFINE_PTR(CallbackReceiverIPtr, CallbackReceiverI);
52 
53 class Application : public Glacier2::Application
54 {
55 public:
56 
Application()57     Application() : _restart(0), _destroyed(false), _receiver(ICE_MAKE_SHARED(CallbackReceiverI))
58     {
59     }
60 
61     virtual Glacier2::SessionPrxPtr
createSession()62     createSession()
63     {
64         return ICE_UNCHECKED_CAST(Glacier2::SessionPrx, router()->createSession("userid", "abc123"));
65     }
66 
67     virtual int
runWithSession(int,char * [])68     runWithSession(int, char*[])
69     {
70         test(router());
71         test(!categoryForClient().empty());
72         test(objectAdapter());
73 
74         if(_restart == 0)
75         {
76             cout << "testing Glacier2::Application restart... " << flush;
77         }
78         Ice::ObjectPrxPtr base = communicator()->stringToProxy(
79             "callback:" + TestHelper::getTestEndpoint(communicator()->getProperties()));
80         CallbackPrxPtr callback = ICE_UNCHECKED_CAST(CallbackPrx, base);
81         if(++_restart < 5)
82         {
83             CallbackReceiverPrxPtr receiver = ICE_UNCHECKED_CAST(CallbackReceiverPrx, addWithUUID(_receiver));
84             callback->initiateCallback(receiver);
85             _receiver->waitForCallback();
86             restart();
87         }
88         cout << "ok" << endl;
89 
90         cout << "testing server shutdown... " << flush;
91         callback->shutdown();
92         cout << "ok" << endl;
93 
94         return 0;
95     }
96 
sessionDestroyed()97     virtual void sessionDestroyed()
98     {
99         _destroyed = true;
100     }
101 
102     int _restart;
103     bool _destroyed;
104     CallbackReceiverIPtr _receiver;
105 };
106 
107 } // anonymous namespace end
108 
109 class Client : public Test::TestHelper
110 {
111 public:
112 
113     void run(int, char**);
114 };
115 
116 void
run(int argc,char ** argv)117 Client::run(int argc, char** argv)
118 {
119     Application app;
120     Ice::InitializationData initData;
121     initData.properties = createTestProperties(argc, argv);
122     initData.properties->setProperty("Ice.Warn.Connections", "0");
123     initData.properties->setProperty("Ice.Default.Router",
124                                      "Glacier2/router:" + TestHelper::getTestEndpoint(initData.properties, 50));
125     int status = app.main(argc, argv, initData);
126     if(status != 0)
127     {
128         test(false);
129     }
130 
131     initData.properties->setProperty("Ice.Default.Router", "");
132 
133     Ice::CommunicatorHolder communicator = initialize(argc, argv, initData);
134 
135     cout << "testing stringToProxy for process object... " << flush;
136     Ice::ObjectPrxPtr processBase = communicator->stringToProxy("Glacier2/admin -f Process:" + getTestEndpoint(51));
137     cout << "ok" << endl;
138 
139     cout << "testing checked cast for admin object... " << flush;
140     Ice::ProcessPrxPtr process = ICE_CHECKED_CAST(Ice::ProcessPrx, processBase);
141     test(process != 0);
142     cout << "ok" << endl;
143 
144     cout << "testing Glacier2 shutdown... " << flush;
145     process->shutdown();
146 
147     try
148     {
149         process->ice_ping();
150         test(false);
151     }
152     catch(const Ice::LocalException&)
153     {
154         cout << "ok" << endl;
155     }
156 
157     test(app._restart == 5);
158     test(app._destroyed);
159 }
160 
161 DEFINE_TEST(Client)
162