Skip to content

Commit

Permalink
TSocket is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
吕海涛 committed Dec 3, 2017
1 parent d5fd7a7 commit 5f4e558
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -32,7 +32,7 @@ use OpenTracing\Formats;
// init factory
$factory = Factory::getInstance();
// make OpenTracing\Tracer instance
$tracer = $factory->initTracer('user');
$tracer = $factory->initTracer('user', '127.0.0.1', 6831);

// extract parent infomation from http header
$carrier = $_SERVER['HTTP_UBER_TRACE_ID'];
Expand Down
2 changes: 1 addition & 1 deletion example/HTTP.php
Expand Up @@ -9,7 +9,7 @@
//init server span start
$factory = Factory::getInstance();

$tracer = $factory->initTracer('gift');
$tracer = $factory->initTracer('gift', '127.0.0.1', 6831);

$trace_id = $_SERVER['HTTP_MY_TRACE_ID'];
$spanContext = $tracer->extract(Formats\BINARY, $trace_id);
Expand Down
2 changes: 1 addition & 1 deletion example/Hprose.php
Expand Up @@ -8,7 +8,7 @@
//init server span start
$factory = Factory::getInstance();

$tracer = $factory->initTracer('user');
$tracer = $factory->initTracer('user', '127.0.0.1', 6831);

$serverSpan = $tracer->startSpan('example HTTP', []);

Expand Down
19 changes: 16 additions & 3 deletions src/Transport/TransportUdp.php
Expand Up @@ -6,7 +6,7 @@
use Jaeger\Thrift\Batch;
use Jaeger\Thrift\Agent\AgentIf;
use Jaeger\Thrift\Agent\AgentClient;
use Thrift\Transport\TSocket;
use Thrift\Transport\TMemoryBuffer;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Protocol\TProtocol;

Expand All @@ -24,6 +24,13 @@ class TransportUdp implements Transport

private $bufferSize = 0;

private $socket;

/**
* @var TMemoryBuffer
*/
private $transport;

/**
* @var TProtocol
*/
Expand Down Expand Up @@ -52,6 +59,8 @@ public function __construct(string $host, int $port, int $maxPacketSize = 0)
$this->host = $host;
$this->port = $port;

$this->socket = fsockopen("udp://$host", $port);

if ($maxPacketSize == 0) {
$maxPacketSize = self::PACKET_MAX_LENGTH;
}
Expand All @@ -61,8 +70,8 @@ public function __construct(string $host, int $port, int $maxPacketSize = 0)
throw new \InvalidArgumentException('$maxPacketSize must be greater than '.self::EMITBATCHOVERHEAD);
}

$socket = new TSocket('udp://'.$host, $port);
$this->agent = new AgentClient(null, new TCompactProtocol($socket));
$this->transport = new TMemoryBuffer;
$this->agent = new AgentClient(null, new TCompactProtocol($this->transport));

$this->protocol = new TCompactProtocol(new TEmptyMemoryBuffer());
}
Expand Down Expand Up @@ -103,7 +112,11 @@ public function flush()
}

$this->agent->emitBatch($this->batch);
$this->batch = null;
$this->bufferSize = 0;

$buf = $this->transport->read(self::PACKET_MAX_LENGTH);
fwrite($this->socket, $buf);
}

public function getBufferSize($thrift)
Expand Down
35 changes: 11 additions & 24 deletions tests/Transport/TransportUdpTest.php
Expand Up @@ -3,10 +3,15 @@

use PHPUnit\Framework\TestCase;
use Mockery as m;
use Thrift\Transport\TMemoryBuffer;
use Jaeger\Thrift\Batch;
use Jaeger\Thrift\Agent\AgentIf;
use Jaeger\Factory;

function fwrite($fp, $buf, $len = 0)
{
}

class TransportUdpTest extends TestCase
{
public function testGetBufferSize()
Expand All @@ -23,46 +28,28 @@ public function testGetBufferSize()
self::assertEquals(6, $size); // size + string
}

public function testFlush()
{
$batch = m::mock(Batch::class);
$agent = m::mock(AgentIf::class);
$agent->shouldReceive('emitBatch')
->once()
->andReturnUsing(function ($_batch) use($batch) {
self::assertSame($batch, $_batch);
});

$transport = new TransportUdp('127.0.0.1', 1024);
$this->setPrivateValue($transport, 'batch', $batch);
$this->setPrivateValue($transport, 'agent', $agent);
$transport->flush();
}

public function testNew()
{
$transport = new TransportUdp('127.0.0.1', 1024);
$agent = $this->getPrivateValue($transport, 'agent');
$protocol = $this->getPrivateValue($agent, 'output_');
/* @var \Thrift\Transport\TSocket $transport */
$transport = $protocol->getTransport();

self::assertEquals('udp://127.0.0.1', $transport->getHost());
self::assertEquals(1024, $transport->getPort());
$socket = $this->getPrivateValue($transport, 'socket');
self::assertEquals('127.0.0.1:1024', stream_socket_get_name($socket, true));
}

public function testAppend()
{
$buf = m::mock(TMemoryBuffer::class)->makePartial();
$buf->shouldReceive('read')->once()->with(65000)->andReturn('hello');
$agent = m::mock(AgentIf::class);
$agent->shouldReceive('emitBatch')
->once()
->andReturnUsing(function ($batch)
{
->andReturnUsing(function ($batch) {
self::assertEquals('demo', $batch->process->serviceName);
self::assertEquals(1, count($batch->spans));
self::assertEquals('foo', $batch->spans[0]->operationName);
});
$transport = new TransportUdp('127.0.0.1', 1024);
$this->setPrivateValue($transport, 'transport', $buf);
$this->setPrivateValue($transport, 'agent', $agent);

$factory = new Factory;
Expand Down

0 comments on commit 5f4e558

Please sign in to comment.