Skip to content

Commit 377c0be

Browse files
committed
+ test for PinbaClient
1 parent ff44a41 commit 377c0be

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

doc/ChangeLog

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
2012-04-12 Evgeny V. Kokovikhin
2+
3+
* test/main/Utils/PinbaTest.class.php, main/Monitoring/PinbaClient.class.php:
4+
test for PinbaClient added. Thanks to Artem A. Naumenko
5+
16
2012-04-11 Evgeny V. Kokovikhin
27

38
* main/Monitoring/PinbaClient.class.php: base ability of
4-
tree (integral) logging added. thanks to Artem A. Naumenko
9+
tree (integral) logging added. Thanks to Artem A. Naumenko
510

611
2012-04-04 Georgiy T. Kutsurua
712

main/Monitoring/PinbaClient.class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function isTreeLogEnabled()
4949
return $this->treeLogEnabled;
5050
}
5151

52+
public function getTreeQueue()
53+
{
54+
return $this->queue;
55+
}
56+
5257
public function timerStart($name, array $tags, array $data = array())
5358
{
5459
if (array_key_exists($name, $this->timers))
@@ -60,7 +65,7 @@ public function timerStart($name, array $tags, array $data = array())
6065
$tags['treeId'] = $id;
6166

6267
if (!empty($this->queue))
63-
list($tags['treeParentId']) = end($this->queue);
68+
$tags['treeParentId'] = end($this->queue);
6469
else
6570
$tags['treeParentId'] = 'root';
6671

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
final class PinbaTest extends TestCase
4+
{
5+
protected function setUp()
6+
{
7+
if (!extension_loaded('pinba'))
8+
$this->markTestSkipped(
9+
'The pinba extension is not available.'
10+
);
11+
12+
if (!PinbaClient::isEnabled())
13+
$this->markTestSkipped(
14+
'The pinba is not enabled at php.ini (pinba.enabled=1).'
15+
);
16+
17+
if (!extension_loaded('runkit')) {
18+
$this->markTestSkipped(
19+
'The runkit extension is not available.'
20+
);
21+
}
22+
23+
if (!ini_get('runkit.internal_override'))
24+
$this->markTestSkipped(
25+
'The runkit.internal_override is not enabled (enabled it at php.ini).'
26+
);
27+
28+
runkit_function_rename('pinba_timer_start', 'pinba_timer_start_bak');
29+
runkit_function_rename('pinba_timer_stop', 'pinba_timer_stop_bak');
30+
31+
runkit_function_rename('pinba_timer_start_callback', 'pinba_timer_start');
32+
runkit_function_rename('pinba_timer_stop_callback', 'pinba_timer_stop');
33+
}
34+
35+
public static function tearDownAfterClass(){
36+
37+
if (
38+
!extension_loaded('runkit')
39+
|| !ini_get('runkit.internal_override')
40+
)
41+
return;
42+
43+
runkit_function_rename('pinba_timer_start', 'pinba_timer_start_callback');
44+
runkit_function_rename('pinba_timer_stop', 'pinba_timer_stop_callback');
45+
46+
runkit_function_rename('pinba_timer_start_bak', 'pinba_timer_start');
47+
runkit_function_rename('pinba_timer_stop_bak', 'pinba_timer_stop');
48+
}
49+
50+
public function testTreeLog()
51+
{
52+
PinbaClient::me()->setTreeLogEnabled();
53+
54+
$this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 0);
55+
56+
PinbaClient::me()->timerStart(
57+
'test',
58+
array("test" => "main")
59+
);
60+
61+
$this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 1);
62+
63+
PinbaClient::me()->timerStart(
64+
'subtest',
65+
array("test" => "submain")
66+
);
67+
68+
$this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 2);
69+
70+
PinbaClient::me()->timerStop('subtest');
71+
72+
$this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 1);
73+
74+
PinbaClient::me()->timerStop('test');
75+
76+
$this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 0);
77+
78+
}
79+
}
80+
81+
final class RunkitCallback
82+
{
83+
public static $queue = array();
84+
public static $log = array();
85+
86+
public static function start($tags, $data = array())
87+
{
88+
self::$log[] = $tags;
89+
end(self::$log);
90+
91+
if (!empty($tags['treeParentId']) && $tags['treeParentId'] != "root") {
92+
if ($tags['treeParentId'] != end(self::$queue)) {
93+
throw new Exception("Error generatin tree");
94+
}
95+
}
96+
97+
if (!empty($tags['treeId'])) {
98+
self::$queue[] = $tags['treeId'];
99+
}
100+
101+
return key(self::$log);
102+
}
103+
104+
public static function stop($id)
105+
{
106+
$current = self::$log[$id];
107+
$tree_id = $current['treeId'];
108+
109+
if (end(self::$queue) != $tree_id) {
110+
throw new Exception("Error generatin tree");
111+
}
112+
113+
array_pop(self::$queue);
114+
unset(self::$log[$id]);
115+
}
116+
}
117+
118+
function pinba_timer_start_callback ($tags, $data = array()) {
119+
return RunkitCallback::start($tags, $data);
120+
}
121+
122+
function pinba_timer_stop_callback($id){
123+
return RunkitCallback::stop($id);
124+
}
125+
?>

0 commit comments

Comments
 (0)