File: gearman_client_integration_test_001.phpt

package info (click to toggle)
php-gearman 2.0.2%2B1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 868 kB
  • ctags: 723
  • sloc: ansic: 7,092; php: 826; xml: 570; sh: 32; makefile: 1
file content (83 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download | duplicates (2)
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
--TEST--

--SKIPIF--
<?php if (!extension_loaded("gearman")) print "skip"; ?>
--FILE--
<?php

$host = 'localhost';
$port = '4730';

$job_name = uniqid();

$pid = pcntl_fork();
if ($pid == -1) {
	die("Could not fork");
} else if ($pid > 0) {
	// Parent. This should be the Worker
	$worker = new GearmanWorker();
	$worker->addServer($host, $port);

	print "addFunction: " . var_export(
		$worker->addFunction(
			$job_name,
			function($job) {
				print "workload: " . var_export($job->workload(), true) . PHP_EOL;
			}
		),
		true
	) . PHP_EOL;

	for($i=0; $i<6; $i++) {
		$worker->work();
	}

	print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL;

	// Wait for child
	$exit_status = 0;
	if (pcntl_wait($exit_status) <= 0) {
		print "pcntl_wait exited with error" . PHP_EOL;
	} else if (!pcntl_wifexited($exit_status)) {
		print "child exited with error" . PHP_EOL;
	}

	print "OK" . PHP_EOL;
} else {
	// Child. This is the Client
	$client = new GearmanClient();
	$client->addServer($host, $port);

	$job_types = ['doNormal', 'doHigh', 'doLow'];
	foreach ($job_types as $job_type) {
		$unique_key = "{$job_name}_{$job_type}";
		$workload = "Workload for $job_type";
		$handle = $client->$job_type($job_name, $workload, $unique_key);
	}

	// Background jobs can run into a race condition if they complete out of
	// order
	$job_types = ['doBackground', 'doHighBackground', 'doLowBackground'];
	foreach ($job_types as $job_type) {
		$unique_key = "{$job_name}_{$job_type}";
		$workload = "Workload for $job_type";
		$handle = $client->$job_type($job_name, $workload, $unique_key);

		do {
			usleep(10000);
			list($is_known, $is_running, $numerator, $denominator) =
				$client->jobStatus($handle);
		} while ($is_known === true || $is_running === true);
	}
}
?>
--EXPECT--
addFunction: true
workload: 'Workload for doNormal'
workload: 'Workload for doHigh'
workload: 'Workload for doLow'
workload: 'Workload for doBackground'
workload: 'Workload for doHighBackground'
workload: 'Workload for doLowBackground'
unregister: true
OK