Port TestExtIpc to php
I don't like these ones very much.
Esse commit está contido em:
@@ -36,7 +36,6 @@
|
||||
#include "hphp/facebook/extensions/urlextraction/test_ext_urlextraction.h"
|
||||
#include "hphp/test/ext/test_ext_curl.h"
|
||||
#include "hphp/test/ext/test_ext_imagesprite.h"
|
||||
#include "hphp/test/ext/test_ext_ipc.h"
|
||||
#include "hphp/test/ext/test_ext_mailparse.h"
|
||||
#include "hphp/test/ext/test_ext_math.h"
|
||||
#include "hphp/test/ext/test_ext_mb.h"
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| HipHop for PHP |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 3.01 of the PHP license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.php.net/license/3_01.txt |
|
||||
| If you did not receive a copy of the PHP license and are unable to |
|
||||
| obtain it through the world-wide-web, please send a note to |
|
||||
| license@php.net so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "hphp/test/ext/test_ext_ipc.h"
|
||||
#include "hphp/runtime/ext/ext_ipc.h"
|
||||
#include <sys/wait.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool TestExtIpc::RunTests(const std::string &which) {
|
||||
bool ret = true;
|
||||
|
||||
/*
|
||||
RUN_TEST(test_ftok);
|
||||
RUN_TEST(test_msg_get_queue);
|
||||
RUN_TEST(test_msg_send);
|
||||
RUN_TEST(test_msg_receive);
|
||||
RUN_TEST(test_msg_remove_queue);
|
||||
RUN_TEST(test_msg_set_queue);
|
||||
RUN_TEST(test_msg_stat_queue);
|
||||
RUN_TEST(test_sem_acquire);
|
||||
RUN_TEST(test_sem_get);
|
||||
RUN_TEST(test_sem_release);
|
||||
RUN_TEST(test_sem_remove);
|
||||
RUN_TEST(test_shm_attach);
|
||||
RUN_TEST(test_shm_detach);
|
||||
RUN_TEST(test_shm_remove);
|
||||
RUN_TEST(test_shm_get_var);
|
||||
RUN_TEST(test_shm_put_var);
|
||||
RUN_TEST(test_shm_remove_var);
|
||||
*/
|
||||
RUN_TEST(test_message_queue);
|
||||
RUN_TEST(test_semaphore);
|
||||
RUN_TEST(test_shared_memory);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool TestExtIpc::test_message_queue() {
|
||||
static const StaticString s_msg_qnum("msg_qnum");
|
||||
char filename[64];
|
||||
strcpy(filename, "/tmp/XXXXXX");
|
||||
close(mkstemp(filename));
|
||||
|
||||
int64_t token = f_ftok(filename, "a");
|
||||
VERIFY(!f_msg_queue_exists(token));
|
||||
Object queue = f_msg_get_queue(token);
|
||||
VERIFY(queue.get());
|
||||
VERIFY(f_msg_queue_exists(token));
|
||||
|
||||
int pid = fork();
|
||||
if (pid == 0) {
|
||||
Object q = f_msg_get_queue(token);
|
||||
always_assert(q.get());
|
||||
always_assert(f_msg_send(q, 2, "start"));
|
||||
Variant type, msg;
|
||||
always_assert(f_msg_receive(q, 1, ref(type), 100, ref(msg)));
|
||||
always_assert(f_msg_send(q, 2, msg)); // echo
|
||||
_exit(-1);
|
||||
}
|
||||
|
||||
Variant type, msg;
|
||||
VERIFY(f_msg_receive(queue, 2, ref(type), 100, ref(msg)));
|
||||
VERIFY(same(msg, "start"));
|
||||
|
||||
VERIFY(f_msg_send(queue, 1, "ok"));
|
||||
VERIFY(f_msg_receive(queue, 2, ref(type), 100, ref(msg)));
|
||||
VERIFY(same(msg, "ok"));
|
||||
|
||||
Array ret = f_msg_stat_queue(queue);
|
||||
VS(ret[s_msg_qnum], 0);
|
||||
f_msg_set_queue(queue, CREATE_MAP1("msg_perm.mode", 0666));
|
||||
|
||||
f_msg_remove_queue(queue);
|
||||
int status = -1;
|
||||
wait(&status);
|
||||
return Count(true);
|
||||
}
|
||||
|
||||
bool TestExtIpc::test_ftok() { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_get_queue() { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_send() { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_receive () { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_remove_queue() { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_set_queue() { return test_message_queue();}
|
||||
bool TestExtIpc::test_msg_stat_queue() { return test_message_queue();}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool TestExtIpc::test_semaphore() {
|
||||
Variant ret = f_sem_get(0xDEADBEEF);
|
||||
VERIFY(!same(ret, false));
|
||||
Object sem = ret.toObject();
|
||||
time_t now = time(0);
|
||||
VERIFY(f_sem_acquire(sem));
|
||||
|
||||
int pid = fork();
|
||||
if (pid == 0) {
|
||||
Variant ret = f_sem_get(0xDEADBEEF);
|
||||
always_assert(!same(ret, false));
|
||||
Object sem = ret.toObject();
|
||||
|
||||
always_assert(f_sem_acquire(sem));
|
||||
|
||||
// This isn't a sure test, but may be false if f_sem_acquire() didn't work
|
||||
time_t then = time(0);
|
||||
always_assert(then - now > 1);
|
||||
(void)then;
|
||||
(void)now;
|
||||
|
||||
always_assert(f_sem_release(sem));
|
||||
VERIFY(f_sem_remove(sem));
|
||||
_exit(-1);
|
||||
}
|
||||
|
||||
sleep(3); // aha
|
||||
VERIFY(f_sem_release(sem));
|
||||
int status = -1;
|
||||
wait(&status);
|
||||
return Count(true);
|
||||
}
|
||||
|
||||
bool TestExtIpc::test_sem_acquire() { return test_semaphore();}
|
||||
bool TestExtIpc::test_sem_get() { return test_semaphore();}
|
||||
bool TestExtIpc::test_sem_release() { return test_semaphore();}
|
||||
bool TestExtIpc::test_sem_remove() { return test_semaphore();}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool TestExtIpc::test_shared_memory() {
|
||||
Variant ret = f_shm_attach(0xDEADBEEF);
|
||||
VERIFY(!same(ret, false));
|
||||
int64_t index = ret.toInt64();
|
||||
VERIFY(!f_shm_has_var(index, 1234));
|
||||
VERIFY(f_shm_put_var(index, 1234, "test"));
|
||||
VERIFY(f_shm_has_var(index, 1234));
|
||||
|
||||
int pid = fork();
|
||||
if (pid == 0) {
|
||||
Variant ret = f_shm_attach(index);
|
||||
always_assert(!same(ret, false));
|
||||
Object mem = ret.toObject();
|
||||
|
||||
ret = f_shm_get_var(index, 1234);
|
||||
always_assert(same(ret, "test"));
|
||||
|
||||
always_assert(f_shm_remove_var(index, 1234));
|
||||
always_assert(f_shm_detach(index));
|
||||
_exit(-1);
|
||||
}
|
||||
|
||||
// Verifying f_shm_remove_var worked, this is not sure test though.
|
||||
ret = f_shm_get_var(index, 1234);
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
if (same(ret, false)) break;
|
||||
usleep(1000);
|
||||
ret = f_shm_get_var(index, 1234);
|
||||
}
|
||||
VERIFY(same(ret, false));
|
||||
|
||||
VERIFY(f_shm_remove(index));
|
||||
int status = -1;
|
||||
wait(&status);
|
||||
return Count(true);
|
||||
}
|
||||
|
||||
bool TestExtIpc::test_shm_attach() { return test_shared_memory();}
|
||||
bool TestExtIpc::test_shm_detach() { return test_shared_memory();}
|
||||
bool TestExtIpc::test_shm_remove() { return test_shared_memory();}
|
||||
bool TestExtIpc::test_shm_get_var() { return test_shared_memory();}
|
||||
bool TestExtIpc::test_shm_put_var() { return test_shared_memory();}
|
||||
bool TestExtIpc::test_shm_remove_var() { return test_shared_memory();}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| HipHop for PHP |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 3.01 of the PHP license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.php.net/license/3_01.txt |
|
||||
| If you did not receive a copy of the PHP license and are unable to |
|
||||
| obtain it through the world-wide-web, please send a note to |
|
||||
| license@php.net so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef incl_HPHP_TEST_EXT_IPC_H_
|
||||
#define incl_HPHP_TEST_EXT_IPC_H_
|
||||
|
||||
// >>>>>> Generated by idl.php. Do NOT modify. <<<<<<
|
||||
|
||||
#include "hphp/test/ext/test_cpp_ext.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class TestExtIpc : public TestCppExt {
|
||||
public:
|
||||
virtual bool RunTests(const std::string &which);
|
||||
|
||||
bool test_ftok();
|
||||
bool test_msg_get_queue();
|
||||
bool test_msg_send();
|
||||
bool test_msg_receive();
|
||||
bool test_msg_remove_queue();
|
||||
bool test_msg_set_queue();
|
||||
bool test_msg_stat_queue();
|
||||
bool test_sem_acquire();
|
||||
bool test_sem_get();
|
||||
bool test_sem_release();
|
||||
bool test_sem_remove();
|
||||
bool test_shm_attach();
|
||||
bool test_shm_detach();
|
||||
bool test_shm_remove();
|
||||
bool test_shm_get_var();
|
||||
bool test_shm_put_var();
|
||||
bool test_shm_remove_var();
|
||||
|
||||
bool test_message_queue();
|
||||
bool test_semaphore();
|
||||
bool test_shared_memory();
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif // incl_HPHP_TEST_EXT_IPC_H_
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
$s_msg_qnum = "msg_qnum";
|
||||
$filename = tempnam('/tmp', 'vmmsgqueue');
|
||||
|
||||
$token = ftok($filename, "a");
|
||||
if (msg_queue_exists($token)) { echo "queue exists\n"; exit(1); }
|
||||
|
||||
$queue = msg_get_queue($token);
|
||||
var_dump(msg_queue_exists($token));
|
||||
|
||||
$pid = pcntl_fork();
|
||||
if ($pid == 0) {
|
||||
$q = msg_get_queue($token);
|
||||
msg_send($q, 2, "start");
|
||||
msg_receive($q, 1, $type, 100, $msg);
|
||||
msg_send($q, 2, $msg); // echo
|
||||
exit(0);
|
||||
}
|
||||
|
||||
msg_receive($queue, 2, $type, 100, $msg);
|
||||
var_dump($msg);
|
||||
|
||||
msg_send($queue, 1, "ok");
|
||||
msg_receive($queue, 2, $type, 100, $msg);
|
||||
var_dump($msg);
|
||||
|
||||
$ret = msg_stat_queue($queue);
|
||||
var_dump($ret[$s_msg_qnum]);
|
||||
msg_set_queue($queue, array("msg_perm.mode" => 0666));
|
||||
|
||||
msg_remove_queue($queue);
|
||||
pcntl_waitpid($pid, $status);
|
||||
|
||||
unlink($filename);
|
||||
@@ -0,0 +1,4 @@
|
||||
bool(true)
|
||||
string(5) "start"
|
||||
string(2) "ok"
|
||||
int(0)
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
$ret = sem_get(0xDEADBEEF);
|
||||
if ($ret === false) { echo "failed\n"; exit(1); }
|
||||
$sem = $ret;
|
||||
$now = microtime(true);
|
||||
var_dump(sem_acquire($sem));
|
||||
|
||||
$pid = pcntl_fork();
|
||||
if ($pid == 0) {
|
||||
$sem = sem_get(0xDEADBEEF);
|
||||
if (sem_acquire($sem) !== true) {
|
||||
echo "oops1\n";
|
||||
}
|
||||
|
||||
// This isn't a sure test, but may be false if sem_acquire() didn't work
|
||||
$then = microtime(true);
|
||||
if (!($then - $now > 1)) {
|
||||
echo "oops: $then $now\n";
|
||||
}
|
||||
|
||||
sem_release($sem);
|
||||
sem_remove($sem);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
sleep(3); // aha
|
||||
sem_release($sem);
|
||||
pcntl_waitpid($pid, $status);
|
||||
@@ -0,0 +1 @@
|
||||
bool(true)
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
$ret = shm_attach(0xDEADBEEF);
|
||||
if ($ret === false) { echo "failed\n"; exit(1); }
|
||||
$index = $ret;
|
||||
var_dump(shm_has_var($index, 1234));
|
||||
shm_put_var($index, 1234, "test");
|
||||
var_dump(shm_has_var($index, 1234));
|
||||
|
||||
$pid = pcntl_fork();
|
||||
if ($pid == 0) {
|
||||
$ret = shm_attach($index);
|
||||
$ret = shm_get_var($index, 1234);
|
||||
if ($ret !== "test") {
|
||||
echo "oops\n";
|
||||
exit(1);
|
||||
}
|
||||
shm_remove_var($index, 1234);
|
||||
shm_detach($index);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Verifying shm_remove_var worked, this is not sure test though.
|
||||
$ret = shm_get_var($index, 1234);
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
if ($ret === false) break;
|
||||
usleep(1000);
|
||||
$ret = shm_get_var($index, 1234);
|
||||
}
|
||||
var_dump($ret === false);
|
||||
|
||||
shm_remove($index);
|
||||
|
||||
pcntl_waitpid($pid, $status);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
bool(false)
|
||||
bool(true)
|
||||
bool(true)
|
||||
Referência em uma Nova Issue
Bloquear um usuário