2011-03-04 00:49:40 +00:00
|
|
|
<?php
|
2012-09-05 19:47:14 +00:00
|
|
|
|
|
|
|
namespace test\php;
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
|
|
|
|
|
|
|
|
use Thrift\ClassLoader\ThriftClassLoader;
|
|
|
|
|
|
|
|
if (!isset($GEN_DIR)) {
|
|
|
|
$GEN_DIR = 'gen-php';
|
|
|
|
}
|
|
|
|
if (!isset($MODE)) {
|
|
|
|
$MODE = 'normal';
|
|
|
|
}
|
|
|
|
|
|
|
|
$loader = new ThriftClassLoader();
|
|
|
|
$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
|
|
|
|
$loader->registerDefinition('ThriftTest', $GEN_DIR);
|
|
|
|
$loader->register();
|
|
|
|
|
2009-03-30 21:35:00 +00:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
/** Include the Thrift base */
|
|
|
|
/** Include the binary protocol */
|
2012-09-05 19:47:14 +00:00
|
|
|
use Thrift\Protocol\TBinaryProtocol;
|
2006-06-30 18:28:50 +00:00
|
|
|
|
|
|
|
/** Include the socket layer */
|
2012-09-05 19:47:14 +00:00
|
|
|
use Thrift\Transport\TSocket;
|
|
|
|
use Thrift\Transport\TSocketPool;
|
2006-06-30 18:28:50 +00:00
|
|
|
|
|
|
|
/** Include the socket layer */
|
2012-09-05 19:47:14 +00:00
|
|
|
use Thrift\Transport\TFramedTransport;
|
|
|
|
use Thrift\Transport\TBufferedTransport;
|
2007-11-13 04:00:29 +00:00
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
$host = 'localhost';
|
|
|
|
$port = 9090;
|
|
|
|
|
|
|
|
if ($argc > 1) {
|
|
|
|
$host = $argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($argc > 2) {
|
|
|
|
$host = $argv[1];
|
|
|
|
}
|
|
|
|
|
2006-10-26 04:56:18 +00:00
|
|
|
$hosts = array('localhost');
|
2006-09-08 03:41:50 +00:00
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
$socket = new TSocket($host, $port);
|
2006-09-08 03:41:50 +00:00
|
|
|
$socket = new TSocketPool($hosts, $port);
|
|
|
|
$socket->setDebug(TRUE);
|
2006-06-30 18:28:50 +00:00
|
|
|
|
2006-09-01 22:19:06 +00:00
|
|
|
if ($MODE == 'inline') {
|
|
|
|
$transport = $socket;
|
2012-09-05 19:47:14 +00:00
|
|
|
$testClient = new \ThriftTest\ThriftTestClient($transport);
|
2011-03-04 00:49:40 +00:00
|
|
|
} else if ($MODE == 'framed') {
|
|
|
|
$framedSocket = new TFramedTransport($socket);
|
|
|
|
$transport = $framedSocket;
|
|
|
|
$protocol = new TBinaryProtocol($transport);
|
2012-09-05 19:47:14 +00:00
|
|
|
$testClient = new \ThriftTest\ThriftTestClient($protocol);
|
2006-09-01 22:19:06 +00:00
|
|
|
} else {
|
|
|
|
$bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
|
|
|
|
$transport = $bufferedSocket;
|
2006-10-26 04:56:18 +00:00
|
|
|
$protocol = new TBinaryProtocol($transport);
|
2012-09-05 19:47:14 +00:00
|
|
|
$testClient = new \ThriftTest\ThriftTestClient($protocol);
|
2006-09-01 22:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$transport->open();
|
2006-06-30 18:28:50 +00:00
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VOID TEST
|
|
|
|
*/
|
|
|
|
print_r("testVoid()");
|
|
|
|
$testClient->testVoid();
|
|
|
|
print_r(" = void\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* STRING TEST
|
|
|
|
*/
|
|
|
|
print_r("testString(\"Test\")");
|
|
|
|
$s = $testClient->testString("Test");
|
|
|
|
print_r(" = \"$s\"\n");
|
2007-11-13 04:00:29 +00:00
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
/**
|
|
|
|
* BYTE TEST
|
|
|
|
*/
|
|
|
|
print_r("testByte(1)");
|
|
|
|
$u8 = $testClient->testByte(1);
|
|
|
|
print_r(" = $u8\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I32 TEST
|
|
|
|
*/
|
|
|
|
print_r("testI32(-1)");
|
|
|
|
$i32 = $testClient->testI32(-1);
|
|
|
|
print_r(" = $i32\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I64 TEST
|
|
|
|
*/
|
|
|
|
print_r("testI64(-34359738368)");
|
|
|
|
$i64 = $testClient->testI64(-34359738368);
|
|
|
|
print_r(" = $i64\n");
|
2006-09-06 02:42:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DOUBLE TEST
|
|
|
|
*/
|
|
|
|
print_r("testDouble(-852.234234234)");
|
|
|
|
$dub = $testClient->testDouble(-852.234234234);
|
|
|
|
print_r(" = $dub\n");
|
2006-06-30 18:28:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* STRUCT TEST
|
|
|
|
*/
|
|
|
|
print_r("testStruct({\"Zero\", 1, -3, -5})");
|
2012-09-05 19:47:14 +00:00
|
|
|
$out = new \ThriftTest\Xtruct();
|
2006-06-30 18:28:50 +00:00
|
|
|
$out->string_thing = "Zero";
|
|
|
|
$out->byte_thing = 1;
|
|
|
|
$out->i32_thing = -3;
|
|
|
|
$out->i64_thing = -5;
|
|
|
|
$in = $testClient->testStruct($out);
|
|
|
|
print_r(" = {\"".$in->string_thing."\", ".
|
|
|
|
$in->byte_thing.", ".
|
|
|
|
$in->i32_thing.", ".
|
|
|
|
$in->i64_thing."}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NESTED STRUCT TEST
|
|
|
|
*/
|
|
|
|
print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
|
2012-09-05 19:47:14 +00:00
|
|
|
$out2 = new \ThriftTest\Xtruct2();
|
2006-06-30 18:28:50 +00:00
|
|
|
$out2->byte_thing = 1;
|
|
|
|
$out2->struct_thing = $out;
|
|
|
|
$out2->i32_thing = 5;
|
|
|
|
$in2 = $testClient->testNest($out2);
|
|
|
|
$in = $in2->struct_thing;
|
|
|
|
print_r(" = {".$in2->byte_thing.", {\"".
|
|
|
|
$in->string_thing."\", ".
|
|
|
|
$in->byte_thing.", ".
|
|
|
|
$in->i32_thing.", ".
|
|
|
|
$in->i64_thing."}, ".
|
|
|
|
$in2->i32_thing."}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MAP TEST
|
|
|
|
*/
|
|
|
|
$mapout = array();
|
|
|
|
for ($i = 0; $i < 5; ++$i) {
|
|
|
|
$mapout[$i] = $i-10;
|
|
|
|
}
|
|
|
|
print_r("testMap({");
|
|
|
|
$first = true;
|
|
|
|
foreach ($mapout as $key => $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r("$key => $val");
|
|
|
|
}
|
|
|
|
print_r("})");
|
|
|
|
|
|
|
|
$mapin = $testClient->testMap($mapout);
|
|
|
|
print_r(" = {");
|
|
|
|
$first = true;
|
|
|
|
foreach ($mapin as $key => $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r("$key => $val");
|
|
|
|
}
|
|
|
|
print_r("}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SET TEST
|
|
|
|
*/
|
|
|
|
$setout = array();;
|
|
|
|
for ($i = -2; $i < 3; ++$i) {
|
|
|
|
$setout []= $i;
|
|
|
|
}
|
|
|
|
print_r("testSet({");
|
|
|
|
$first = true;
|
|
|
|
foreach ($setout as $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r($val);
|
|
|
|
}
|
|
|
|
print_r("})");
|
|
|
|
$setin = $testClient->testSet($setout);
|
|
|
|
print_r(" = {");
|
|
|
|
$first = true;
|
|
|
|
foreach ($setin as $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r($val);
|
|
|
|
}
|
|
|
|
print_r("}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LIST TEST
|
|
|
|
*/
|
|
|
|
$listout = array();
|
|
|
|
for ($i = -2; $i < 3; ++$i) {
|
|
|
|
$listout []= $i;
|
|
|
|
}
|
|
|
|
print_r("testList({");
|
|
|
|
$first = true;
|
|
|
|
foreach ($listout as $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r($val);
|
|
|
|
}
|
|
|
|
print_r("})");
|
|
|
|
$listin = $testClient->testList($listout);
|
|
|
|
print_r(" = {");
|
|
|
|
$first = true;
|
|
|
|
foreach ($listin as $val) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
print_r(", ");
|
|
|
|
}
|
|
|
|
print_r($val);
|
|
|
|
}
|
|
|
|
print_r("}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ENUM TEST
|
|
|
|
*/
|
|
|
|
print_r("testEnum(ONE)");
|
2011-03-02 18:13:30 +00:00
|
|
|
$ret = $testClient->testEnum(ThriftTest_Numberz::ONE);
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r(" = $ret\n");
|
|
|
|
|
|
|
|
print_r("testEnum(TWO)");
|
2011-03-02 18:13:30 +00:00
|
|
|
$ret = $testClient->testEnum(ThriftTest_Numberz::TWO);
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r(" = $ret\n");
|
|
|
|
|
|
|
|
print_r("testEnum(THREE)");
|
2011-03-02 18:13:30 +00:00
|
|
|
$ret = $testClient->testEnum(ThriftTest_Numberz::THREE);
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r(" = $ret\n");
|
|
|
|
|
|
|
|
print_r("testEnum(FIVE)");
|
2011-03-02 18:13:30 +00:00
|
|
|
$ret = $testClient->testEnum(ThriftTest_Numberz::FIVE);
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r(" = $ret\n");
|
|
|
|
|
|
|
|
print_r("testEnum(EIGHT)");
|
2011-03-02 18:13:30 +00:00
|
|
|
$ret = $testClient->testEnum(ThriftTest_Numberz::EIGHT);
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r(" = $ret\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TYPEDEF TEST
|
|
|
|
*/
|
|
|
|
print_r("testTypedef(309858235082523)");
|
|
|
|
$uid = $testClient->testTypedef(309858235082523);
|
|
|
|
print_r(" = $uid\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NESTED MAP TEST
|
|
|
|
*/
|
|
|
|
print_r("testMapMap(1)");
|
|
|
|
$mm = $testClient->testMapMap(1);
|
|
|
|
print_r(" = {");
|
|
|
|
foreach ($mm as $key => $val) {
|
|
|
|
print_r("$key => {");
|
|
|
|
foreach ($val as $k2 => $v2) {
|
|
|
|
print_r("$k2 => $v2, ");
|
|
|
|
}
|
|
|
|
print_r("}, ");
|
|
|
|
}
|
|
|
|
print_r("}\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INSANITY TEST
|
|
|
|
*/
|
2012-09-05 19:47:14 +00:00
|
|
|
$insane = new \ThriftTest\Insanity();
|
2011-03-02 18:13:30 +00:00
|
|
|
$insane->userMap[ThriftTest_Numberz::FIVE] = 5000;
|
2012-09-05 19:47:14 +00:00
|
|
|
$truck = new \ThriftTest\Xtruct();
|
2006-06-30 18:28:50 +00:00
|
|
|
$truck->string_thing = "Truck";
|
|
|
|
$truck->byte_thing = 8;
|
|
|
|
$truck->i32_thing = 8;
|
|
|
|
$truck->i64_thing = 8;
|
|
|
|
$insane->xtructs []= $truck;
|
|
|
|
print_r("testInsanity()");
|
|
|
|
$whoa = $testClient->testInsanity($insane);
|
|
|
|
print_r(" = {");
|
|
|
|
foreach ($whoa as $key => $val) {
|
|
|
|
print_r("$key => {");
|
|
|
|
foreach ($val as $k2 => $v2) {
|
|
|
|
print_r("$k2 => {");
|
|
|
|
$userMap = $v2->userMap;
|
|
|
|
print_r("{");
|
2011-03-04 00:49:40 +00:00
|
|
|
if (is_array($userMap)) {
|
2006-09-01 22:19:06 +00:00
|
|
|
foreach ($userMap as $k3 => $v3) {
|
|
|
|
print_r("$k3 => $v3, ");
|
|
|
|
}
|
2006-06-30 18:28:50 +00:00
|
|
|
}
|
|
|
|
print_r("}, ");
|
2007-11-13 04:00:29 +00:00
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
$xtructs = $v2->xtructs;
|
|
|
|
print_r("{");
|
2006-09-01 22:19:06 +00:00
|
|
|
if (is_array($xtructs)) {
|
|
|
|
foreach ($xtructs as $x) {
|
|
|
|
print_r("{\"".$x->string_thing."\", ".
|
|
|
|
$x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
|
|
|
|
}
|
2006-06-30 18:28:50 +00:00
|
|
|
}
|
|
|
|
print_r("}");
|
2007-11-13 04:00:29 +00:00
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
print_r("}, ");
|
|
|
|
}
|
|
|
|
print_r("}, ");
|
|
|
|
}
|
|
|
|
print_r("}\n");
|
|
|
|
|
2006-09-01 22:19:06 +00:00
|
|
|
/**
|
|
|
|
* EXCEPTION TEST
|
|
|
|
*/
|
|
|
|
print_r("testException('Xception')");
|
|
|
|
try {
|
|
|
|
$testClient->testException('Xception');
|
|
|
|
print_r(" void\nFAILURE\n");
|
2011-03-04 00:49:40 +00:00
|
|
|
} catch (ThriftTest_Xception $x) {
|
2006-09-01 22:19:06 +00:00
|
|
|
print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
|
|
|
|
}
|
|
|
|
|
2006-06-30 18:28:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normal tests done.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$stop = microtime(true);
|
|
|
|
$elp = round(1000*($stop - $start), 0);
|
|
|
|
print_r("Total time: $elp ms\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extraneous "I don't trust PHP to pack/unpack integer" tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Max I32
|
|
|
|
$num = pow(2, 30) + (pow(2, 30) - 1);
|
|
|
|
$num2 = $testClient->testI32($num);
|
|
|
|
if ($num != $num2) {
|
|
|
|
print "Missed $num = $num2\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Min I32
|
|
|
|
$num = 0 - pow(2, 31);
|
|
|
|
$num2 = $testClient->testI32($num);
|
|
|
|
if ($num != $num2) {
|
|
|
|
print "Missed $num = $num2\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max I64
|
|
|
|
$num = pow(2, 62) + (pow(2, 62) - 1);
|
|
|
|
$num2 = $testClient->testI64($num);
|
|
|
|
if ($num != $num2) {
|
|
|
|
print "Missed $num = $num2\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Min I64
|
|
|
|
$num = 0 - pow(2, 63);
|
|
|
|
$num2 = $testClient->testI64($num);
|
|
|
|
if ($num != $num2) {
|
|
|
|
print "Missed $num = $num2\n";
|
|
|
|
}
|
|
|
|
|
2006-09-01 22:19:06 +00:00
|
|
|
$transport->close();
|
2006-06-30 18:28:50 +00:00
|
|
|
return;
|
|
|
|
|