summaryrefslogtreecommitdiffstats
path: root/tests/testremote.cc
blob: 85bdfb2cf1377f0682c6563e75791cbb2ea66ab4 (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
    /*

    Copyright (C) 2000 Stefan Westerfeld stefan@space.twc.de

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    Permission is also granted to link this program with the Qt
    library, treating Qt like a library that normally accompanies the
    operating system kernel, whether or not that is in fact the case.

    */

#include "remotetest.h"
#include "value.h"
#include "test.h"
#include "connect.h"
#include <iostream.h>
#include <stdio.h>

using namespace std;
using namespace Arts;

class RemoteTest_impl: virtual public RemoteTest_skel {
private:
	bool alive;

public:
	RemoteTest_impl()
	{
		alive = true;
		_copy();
	}
	~RemoteTest_impl()
	{
		Dispatcher::the()->terminate();
	}

	Object createObject(const string& name)
	{
		return SubClass(name);
	}
	long fortyTwo()
	{
		return 42;
	}
	void die()
	{
		if(alive)
		{
			_release();
			alive = false;
		}
	}
};

REGISTER_IMPLEMENTATION(RemoteTest_impl);

/* name of the remote object - should be sufficiently unique */
char objectName[60];

struct TestRemote : public TestCase
{
	TESTCASE(TestRemote);

	Arts::Dispatcher dispatcher;
	RemoteTest remoteTest;

	void setUp() {
		string globalRef = "global:";
		globalRef += objectName;

		remoteTest = Arts::Reference(globalRef);
	}
	void tearDown() {
		remoteTest = RemoteTest::null();
	}

	void process()
	{
		dispatcher.ioManager()->processOneEvent(false);
	}


	/* test whether the server is running, and we could connect ok */
	TEST(connected) {
		testEquals(false, remoteTest.isNull());
	}

	/* test method call */
	TEST(fortyTwo) {
		testEquals(42, remoteTest.fortyTwo());
	}

	/* test remote change notifications */
	TEST(remoteChangeNotify) {
		StringValue local;
		local.value("local");
		testEquals("local", local.value());

		Object o = remoteTest.createObject("StringValue");
		testAssert(!o.isNull());

		StringValue remote = Arts::DynamicCast(o);
		testAssert(!remote.isNull());
		remote.value("remote");
		testEquals("remote", remote.value());

		connect(local,"value_changed",remote,"value");
		local.value("transferme");

		process();
		sleep(1);	/* change notifications are asynchronous */

		testEquals("transferme", local.value());
		testEquals("transferme", remote.value());

		disconnect(local,"value_changed",remote,"value");

		local.value("notransfer");

		testEquals("notransfer", local.value());
		testEquals("transferme", remote.value());
	}

	/* terminate server */
	TEST(die) {
		remoteTest.die();
		remoteTest = RemoteTest::null();
		sleep(1);
	}

	/* check if server is dead now */
	TEST(dead) {
		testEquals(true, remoteTest.isNull());
	}
};

TESTMAINFUNC(TestRemote,performTests);

/*
 * this test case is a bit tricky because we need a client and a server
 *
 * we create the server in a fork()ed process, and then evaluate the test
 * inside the client
 */
int main()
{
	/* create unique object name for test server/test client */
	sprintf(objectName, "RemoteTest_%d_%ld", getpid(), time(0));

	int pid = fork();
	if(pid == 0)	/* child process - we play the server here */
	{
		Dispatcher dispatcher(0, Dispatcher::startUnixServer);
		ObjectManager::the()->addGlobalReference(RemoteTest(), objectName);
		dispatcher.run();
		return 0;
	}
	else if(pid >= 0)	/* parent process - do the test here */
	{
		sleep(1);
		return performTests();
	}
}