File: gearman_tasks_integration_test_001.phpt

package info (click to toggle)
php-gearman 2.0.5%2B1.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 916 kB
  • sloc: ansic: 7,140; php: 826; xml: 572; sh: 32; makefile: 1
file content (79 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (5)
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
--TEST--
Test Gearman worker methods
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnect.inc');
?>
--FILE--
<?php
require_once('connect.inc');

print "Start" . PHP_EOL;

$job_name = uniqid();

$pid = pcntl_fork();
if ($pid == -1) {
    die("Could not fork");
} else if ($pid > 0) {
    // Parent. This is the worker
    $worker = new GearmanWorker();
    print "addServer: " . var_export($worker->addServer($host, $port), true) . PHP_EOL;
    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;
    }
} else {
    //Child. This is the client. Don't echo anything here
    $client = new GearmanClient();
    if ($client->addServer($host, $port) !== true) {
        exit(1); // error
    };

    $tasks = [];
    $tasks[] = $client->addTask($job_name, "normal");
    $tasks[] = $client->addTaskBackground($job_name, "normalbg");
    $tasks[] = $client->addTaskHigh($job_name, 1);
    $tasks[] = $client->addTaskHighBackground($job_name, 2.0);
    $tasks[] = $client->addTaskLow($job_name, "low");
    $tasks[] = $client->addTaskLowBackground($job_name, true);
    $client->runTasks();
    if ($client->returnCode() != GEARMAN_SUCCESS) {
        exit(2); // error
    }
    exit(0);
}

print "Done";
--EXPECTF--
Start
addServer: true
addFunction: true
workload: '2'
workload: '1'
workload: 'normalbg'
workload: 'normal'
workload: '1'
workload: 'low'
unregister: true
Done