2
2
3
3
const WORKER_ARGV_VALUE = 'RUN_WORKER ' ;
4
4
5
- function phpt_notify ()
5
+ const WORKER_DEFAULT_NAME = 'server ' ;
6
+
7
+ function phpt_notify ($ worker = WORKER_DEFAULT_NAME )
6
8
{
7
- ServerClientTestCase::getInstance ()->notify ();
9
+ ServerClientTestCase::getInstance ()->notify ($ worker );
8
10
}
9
11
10
- function phpt_wait ()
12
+ function phpt_wait ($ worker = WORKER_DEFAULT_NAME )
11
13
{
12
- ServerClientTestCase::getInstance ()->wait ();
14
+ ServerClientTestCase::getInstance ()->wait ($ worker );
13
15
}
14
16
15
17
/**
@@ -20,11 +22,11 @@ class ServerClientTestCase
20
22
{
21
23
private $ isWorker = false ;
22
24
23
- private $ workerHandle ;
25
+ private $ workerHandle = [] ;
24
26
25
- private $ workerStdIn ;
27
+ private $ workerStdIn = [] ;
26
28
27
- private $ workerStdOut ;
29
+ private $ workerStdOut = [] ;
28
30
29
31
private static $ instance ;
30
32
@@ -46,26 +48,41 @@ class ServerClientTestCase
46
48
$ this ->isWorker = $ isWorker ;
47
49
}
48
50
49
- private function spawnWorkerProcess ($ code )
51
+ private function spawnWorkerProcess ($ worker , $ code )
50
52
{
51
53
if (defined ("PHP_WINDOWS_VERSION_MAJOR " )) {
52
- $ ini = php_ini_loaded_file ();
53
- $ cmd = sprintf ('%s %s "%s" %s ' , PHP_BINARY , $ ini ? "-n -c $ ini " : "" , __FILE__ , WORKER_ARGV_VALUE );
54
+ $ ini = php_ini_loaded_file ();
55
+ $ cmd = sprintf (
56
+ '%s %s "%s" %s ' ,
57
+ PHP_BINARY , $ ini ? "-n -c $ ini " : "" ,
58
+ __FILE__ ,
59
+ WORKER_ARGV_VALUE
60
+ );
54
61
} else {
55
- $ cmd = sprintf ('%s "%s" %s ' , PHP_BINARY , __FILE__ , WORKER_ARGV_VALUE );
62
+ $ cmd = sprintf (
63
+ '%s "%s" %s %s ' ,
64
+ PHP_BINARY ,
65
+ __FILE__ ,
66
+ WORKER_ARGV_VALUE ,
67
+ $ worker
68
+ );
56
69
}
57
- $ this ->workerHandle = proc_open ($ cmd , [['pipe ' , 'r ' ], ['pipe ' , 'w ' ], STDERR ], $ pipes );
58
- $ this ->workerStdIn = $ pipes [0 ];
59
- $ this ->workerStdOut = $ pipes [1 ];
60
-
61
- fwrite ($ this ->workerStdIn , $ code . "\n--- \n" );
70
+ $ this ->workerHandle [$ worker ] = proc_open (
71
+ $ cmd ,
72
+ [['pipe ' , 'r ' ], ['pipe ' , 'w ' ], STDERR ],
73
+ $ pipes
74
+ );
75
+ $ this ->workerStdIn [$ worker ] = $ pipes [0 ];
76
+ $ this ->workerStdOut [$ worker ] = $ pipes [1 ];
77
+
78
+ fwrite ($ this ->workerStdIn [$ worker ], $ code . "\n--- \n" );
62
79
}
63
80
64
- private function cleanupWorkerProcess ()
81
+ private function cleanupWorkerProcess ($ worker )
65
82
{
66
- fclose ($ this ->workerStdIn );
67
- fclose ($ this ->workerStdOut );
68
- proc_close ($ this ->workerHandle );
83
+ fclose ($ this ->workerStdIn [ $ worker ] );
84
+ fclose ($ this ->workerStdOut [ $ worker ] );
85
+ proc_close ($ this ->workerHandle [ $ worker ] );
69
86
}
70
87
71
88
private function stripPhpTagsFromCode ($ code )
@@ -90,21 +107,28 @@ class ServerClientTestCase
90
107
eval ($ code );
91
108
}
92
109
93
- public function run ($ proc1Code , $ proc2Code )
110
+ public function run ($ masterCode , $ workerCode )
94
111
{
95
- $ this ->spawnWorkerProcess ($ this ->stripPhpTagsFromCode ($ proc2Code ));
96
- eval ($ this ->stripPhpTagsFromCode ($ proc1Code ));
97
- $ this ->cleanupWorkerProcess ();
112
+ if (!is_array ($ workerCode )) {
113
+ $ workerCode = [WORKER_DEFAULT_NAME => $ workerCode ];
114
+ }
115
+ foreach ($ workerCode as $ worker => $ code ) {
116
+ $ this ->spawnWorkerProcess ($ worker , $ this ->stripPhpTagsFromCode ($ code ));
117
+ }
118
+ eval ($ this ->stripPhpTagsFromCode ($ masterCode ));
119
+ foreach ($ workerCode as $ worker => $ code ) {
120
+ $ this ->cleanupWorkerProcess ($ worker );
121
+ }
98
122
}
99
123
100
- public function wait ()
124
+ public function wait ($ worker )
101
125
{
102
- fgets ($ this ->isWorker ? STDIN : $ this ->workerStdOut );
126
+ fgets ($ this ->isWorker ? STDIN : $ this ->workerStdOut [ $ worker ] );
103
127
}
104
128
105
- public function notify ()
129
+ public function notify ($ worker )
106
130
{
107
- fwrite ($ this ->isWorker ? STDOUT : $ this ->workerStdIn , "\n" );
131
+ fwrite ($ this ->isWorker ? STDOUT : $ this ->workerStdIn [ $ worker ] , "\n" );
108
132
}
109
133
}
110
134
0 commit comments