File: TestSupport.h

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (262 lines) | stat: -rw-r--r-- 5,819 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef _TEST_SUPPORT_H_
#define _TEST_SUPPORT_H_

#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <vector>
#include <exception>
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <ctime>
#include <unistd.h>
#include <utime.h>

#include <oxt/thread.hpp>
#include <oxt/tracable_exception.hpp>

#include "../tut/tut.h"
#include "ServerInstanceDir.h"
#include "Exceptions.h"
#include "Utils.h"
#include "Utils/SystemTime.h"

namespace TestSupport {

using namespace std;
using namespace Passenger;
using namespace oxt;


#define SHOW_EXCEPTION_BACKTRACE(code)                                \
	do {                                                          \
		try {                                                 \
			code                                          \
		} catch (const tracable_exception &e) {               \
			cerr << e.what() << "\n" << e.backtrace();    \
			throw;                                        \
		}                                                     \
	} while (0)

#define EVENTUALLY(deadline, code)					\
	do {								\
		time_t deadlineTime = time(NULL) + deadline;		\
		bool result = false;					\
		while (!result && time(NULL) < deadlineTime) {		\
			code						\
			if (!result) {					\
				usleep(10000);				\
			}						\
		}							\
		if (!result) {						\
			fail("EVENTUALLY(" #code ") failed");		\
		}							\
	} while (0)

#define SHOULD_NEVER_HAPPEN(deadline, code)						\
	do {										\
		unsigned long long deadlineTime = SystemTime::getMsec(true) + deadline;	\
		bool result = false;							\
		while (!result && SystemTime::getMsec(true) < deadlineTime) {		\
			code								\
			if (!result) {							\
				usleep(10000);						\
			}								\
		}									\
		if (result) {								\
			fail("SHOULD_NEVER_HAPPEN(" #code ") failed");			\
		}									\
	} while (0)


/**
 * Create a server instance directory and generation with default parameters,
 * suitable for unit testing.
 */
void createServerInstanceDirAndGeneration(ServerInstanceDirPtr &serverInstanceDir,
                                          ServerInstanceDir::GenerationPtr &generation);

/**
 * Read all data from the given file until EOF.
 *
 * @throws SystemException
 */
string readAll(const string &filename);

/**
 * Read all data from the given file descriptor until EOF.
 *
 * @throws SystemException
 */
string readAll(int fd);

/**
 * Writes zeroes into the given file descriptor its buffer is full (i.e.
 * the next write will block).
 *
 * @throws SystemException
 */
void writeUntilFull(int fd);

/**
 * Look for 'toFind' inside 'str', replace it with 'replaceWith' and return the result.
 * Only the first occurence of 'toFind' is replaced.
 */
string replaceString(const string &str, const string &toFind, const string &replaceWith);

/**
 * Look for 'toFind' inside the given file, replace it with 'replaceWith' and write
 * the result back to the file. Only the first occurence of 'toFind' is replaced.
 *
 * @throws FileSystemException
 */
void replaceStringInFile(const char *filename, const string &toFind, const string &replaceWith);

/**
 * Writes the given data into the given file.
 *
 * @throws FileSystemException
 */
void writeFile(const string &filename, const string &contents);

/**
 * Touch the given file: create the file if it doesn't exist, update its
 * timestamp if it does. If the <tt>timestamp</tt> argument is -1, then
 * the current system time will be used, otherwise the given timestamp
 * will be used.
 *
 * @throws FileSystemException
 */
void touchFile(const char *filename, time_t timestamp = (time_t) - 1);

/**
 * Returns all filenames in the given directory.
 */
vector<string> listDir(const string &path);

/**
 * Returns the name of the primary group of the given user.
 */
string getPrimaryGroupName(const string &username);


/**
 * Class which creates a temporary directory of the given name, and deletes
 * it upon destruction.
 */
class TempDir {
private:
	string name;
public:
	TempDir(const string &name) {
		this->name = name;
		if (mkdir(name.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
			int e = errno;
			string message = "Cannot create directory '";
			message.append(name);
			message.append("'");
			throw FileSystemException(message, e, name);
		}
	}
	
	~TempDir() {
		removeDirTree(name);
	}
};


/**
 * Creates a temporary copy of the given directory. This copy is deleted
 * upon object destruction.
 */
class TempDirCopy {
private:
	string dir;
public:
	TempDirCopy(const string &source, const string &dest) {
		dir = dest;
		removeDirTree(dest);
		
		char command[1024];
		snprintf(command, sizeof(command), "cp -pR \"%s\" \"%s\"",
			source.c_str(), dest.c_str());
		system(command);
	}
	
	~TempDirCopy() {
		removeDirTree(dir);
	}
};


/**
 * Class which deletes the given file upon destruction.
 */
class DeleteFileEventually {
private:
	string filename;
public:
	DeleteFileEventually(const string &filename) {
		this->filename = filename;
	}
	
	~DeleteFileEventually() {
		unlink(filename.c_str());
	}
};


/**
 * Spawns a thread which will be interrupted and joined when this TempThread
 * object is destroyed.
 */
class TempThread {
public:
	oxt::thread thread;
	
	TempThread(boost::function<void ()> func)
		: thread(func)
		{ }
	
	~TempThread() {
		thread.interrupt_and_join();
	}
};


class AtomicInt {
private:
	mutable boost::mutex lock;
	int val;
public:
	AtomicInt() {
		val = 0;
	}
	
	int get() const {
		lock_guard<boost::mutex> l(lock);
		return val;
	}
	
	void set(int value) {
		lock_guard<boost::mutex> l(lock);
		val = value;
	}
	
	AtomicInt &operator=(int value) {
		set(value);
		return *this;
	}
	
	operator int() const {
		return get();
	}
};

} // namespace TestSupport

using namespace TestSupport;

#endif /* _TEST_SUPPORT_H_ */