Skip to content

Commit bbf5d45

Browse files
author
Dan Richelson
committed
Merge branch 'master' into dr/monologVersion
2 parents fac0b40 + 440f7dd commit bbf5d45

File tree

8 files changed

+188
-38
lines changed

8 files changed

+188
-38
lines changed

circle.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ machine:
22
php:
33
version: 5.6.17
44
services:
5+
- redis
56
- docker
67
dependencies:
78
pre:
9+
- yes '' | pecl install -f apcu-4.0.10
10+
- echo "extension=apcu.so" | sudo tee -a /opt/circleci/php/$(phpenv global)/etc/php.ini
11+
- echo "apc.enable_cli = 1" | sudo tee -a /opt/circleci/php/$(phpenv global)/etc/php.ini
812
- docker pull php
913
- docker pull nyanpass/php5.5
1014

1115
test:
1216
override:
1317
- vendor/bin/phpunit tests --coverage-text
18+
- ln -s vendor integration-tests/vendor
19+
- vendor/bin/phpunit integration-tests/LDDFeatureRequesterTest.php
20+
1421
- composer update && vendor/bin/phpunit tests
1522
- composer update --prefer-lowest && vendor/bin/phpunit tests
1623

integration-tests/LDDFeatureRequesterTest.php

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use LaunchDarkly\LDClient;
77
use LaunchDarkly\LDUserBuilder;
8+
use Predis\Client;
89

910
class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase {
1011

@@ -24,6 +25,9 @@ public function testGet() {
2425
}
2526

2627
public function testGetApc() {
28+
if (!extension_loaded('apc')) {
29+
self::markTestSkipped('Install `apc` extension to run this test.');
30+
}
2731
$redis = new \Predis\Client(array(
2832
"scheme" => "tcp",
2933
"host" => 'localhost',
@@ -46,20 +50,87 @@ public function testGetApc() {
4650
$this->assertEquals("baz", $client->variation('foo', $user, 'jim'));
4751
}
4852

53+
public function testGetApcu() {
54+
if (!extension_loaded('apcu')) {
55+
self::markTestSkipped('Install `apcu` extension to run this test.');
56+
}
57+
58+
$redis = new Client([
59+
'scheme' => 'tcp',
60+
'host' => 'localhost',
61+
'port' => 6379
62+
]);
63+
64+
$client = new LDClient('BOGUS_API_KEY', [
65+
'feature_requester_class' => '\LaunchDarkly\ApcuLDDFeatureRequester',
66+
'apc_expiration' => 1
67+
]);
68+
69+
$builder = new LDUserBuilder(3);
70+
$user = $builder->build();
71+
72+
$redis->del('launchdarkly:features');
73+
$this->assertEquals('alice', $client->variation('fiz', $user, 'alice'));
74+
$redis->hset('launchdarkly:features', 'fiz', $this->gen_feature('fiz', 'buz'));
75+
$this->assertEquals('buz', $client->variation('fiz', $user, 'alice'));
76+
77+
# cached value so not updated
78+
$redis->hset('launchdarkly:features', 'fiz', $this->gen_feature('fiz', 'bob'));
79+
$this->assertEquals('buz', $client->variation('fiz', $user, 'alice'));
80+
81+
\apcu_delete('launchdarkly:features.fiz');
82+
$this->assertEquals('bob', $client->variation('fiz', $user, 'alice'));
83+
}
84+
4985
private function gen_feature($key, $val) {
50-
$data = <<<EOF
51-
{"name": "Feature $key", "key": "$key", "kind": "flag", "salt": "Zm9v", "on": true,
52-
"variations": [{"value": "$val", "weight": 100,
53-
"targets": [{"attribute": "key", "op": "in", "values": []}],
54-
"userTarget": {"attribute": "key", "op": "in", "values": []}},
55-
{"value": false, "weight": 0,
56-
"targets": [{"attribute": "key", "op": "in", "values": []}],
57-
"userTarget": {"attribute": "key", "op": "in", "values": []}}],
58-
"commitDate": "2015-09-08T21:24:16.712Z",
59-
"creationDate": "2015-09-08T21:06:16.527Z",
60-
"version": 4}
61-
EOF;
62-
return $data;
86+
$data = [
87+
'name' => 'Feature ' . $key,
88+
'key' => $key,
89+
'kind' => 'flag',
90+
'salt' => 'Zm9v',
91+
'on' => true,
92+
'variations' => [
93+
$val,
94+
false,
95+
],
96+
'commitDate' => '2015-09-08T21:24:16.712Z',
97+
'creationDate' => '2015-09-08T21:06:16.527Z',
98+
'version' => 4,
99+
'prerequisites' => [],
100+
'targets' => [
101+
[
102+
'values' => [
103+
$val,
104+
],
105+
'variation' => 0,
106+
],
107+
[
108+
'values' => [
109+
false,
110+
],
111+
'variation' => 1,
112+
],
113+
],
114+
'rules' => [],
115+
'fallthrough' => [
116+
'rollout' => [
117+
'variations' => [
118+
[
119+
'variation' => 0,
120+
'weight' => 95000,
121+
],
122+
[
123+
'variation' => 1,
124+
'weight' => 5000,
125+
],
126+
],
127+
],
128+
],
129+
'offVariation' => null,
130+
'deleted' => false,
131+
];
132+
133+
return \json_encode($data);
63134
}
64135

65136
}

integration-tests/Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
1717
config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/trusty64"
1818

1919
config.vm.provision :shell, path: "bootstrap.sh"
20+
config.vm.provision :shell, path: "bootstrap.user.sh", privileged: false
2021

2122
# Create a forwarded port mapping which allows access to a specific port
2223
# within the machine from a port on the host machine. In the example below,

integration-tests/bootstrap.sh

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
2+
set -uxe
33
# init
44
apt-get update 2> /dev/null
55

@@ -36,24 +36,6 @@ set tabstop=4
3636
EOF
3737
chown vagrant.vagrant /home/vagrant/.vimrc
3838

39-
su - vagrant
40-
cd ~vagrant
41-
pwd
42-
curl -s -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
43-
chmod +x phpbrew
44-
sudo mv phpbrew /usr/bin/phpbrew
45-
phpbrew init
46-
phpbrew known --update
47-
phpbrew update
48-
phpbrew install 5.4.34 +default
49-
50-
echo "source $HOME/.phpbrew/bashrc" >> /home/vagrant/.bashrc
51-
source $HOME/.bashrc
52-
phpbrew switch php-5.4.34
53-
phpbrew ext install apc
54-
echo "apc.enable_cli = 1" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini
55-
56-
57-
cd /home/vagrant/project/integration-tests
58-
curl -sS https://getcomposer.org/installer | php
59-
php composer.phar install
39+
# enable APC for php5 CLI
40+
echo "apc.enable_cli = 1" >> /etc/php5/cli/conf.d/20-apcu.ini
41+
php -i | grep apc
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
set -uxe
3+
4+
echo
5+
echo "install phpbrew and php54"
6+
cd ~
7+
curl -s -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
8+
chmod +x phpbrew
9+
sudo mv phpbrew /usr/bin/phpbrew
10+
phpbrew init
11+
phpbrew known --update
12+
phpbrew update
13+
phpbrew install 5.4.34 +default
14+
15+
echo
16+
echo "switch php54"
17+
echo "source $HOME/.phpbrew/bashrc" >> /home/vagrant/.bashrc
18+
source $HOME/.phpbrew/bashrc
19+
phpbrew switch 5.4.34
20+
21+
echo
22+
echo "install php54-apc"
23+
phpbrew ext install apc
24+
echo "apc.enable_cli = 1" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini
25+
php -i | grep apc
26+
echo "date.timezone =UTC" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini
27+
28+
echo
29+
echo "update project dependencies"
30+
cd /home/vagrant/project/integration-tests
31+
curl -sS https://getcomposer.org/installer | php
32+
php composer.phar update

src/LaunchDarkly/ApcLDDFeatureRequester.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
/**
66
* Feature requester from an LDD-populated redis, with APC caching
7+
* @deprecated Per the docs (http://php.net/manual/en/intro.apc.php):
8+
* "This extension (APC) is considered unmaintained and dead".
9+
* Install APCu and use \LaunchDarkly\ApcuLDDFeatureRequester instead!
710
*
811
* @package LaunchDarkly
912
*/
@@ -18,10 +21,19 @@ function __construct($baseUri, $apiKey, $options) {
1821
}
1922
}
2023

24+
/**
25+
* @param $key
26+
* @param $success
27+
* @return mixed
28+
*/
29+
protected function fetch($key, &$success = null)
30+
{
31+
return \apc_fetch($key, $success);
32+
}
2133

2234
protected function get_from_cache($key) {
2335
$key = self::make_cache_key($key);
24-
$enabled = apc_fetch($key);
36+
$enabled = $this->fetch($key);
2537
if ($enabled === false) {
2638
return null;
2739
}
@@ -30,8 +42,19 @@ protected function get_from_cache($key) {
3042
}
3143
}
3244

45+
/**
46+
* @param $key
47+
* @param $var
48+
* @param int $ttl
49+
* @return mixed
50+
*/
51+
protected function add($key, $var, $ttl = 0)
52+
{
53+
return \apc_add($key, $var, $ttl);
54+
}
55+
3356
protected function store_in_cache($key, $val) {
34-
apc_add($this->make_cache_key($key), $val, $this->_expiration);
57+
$this->add($this->make_cache_key($key), $val, $this->_expiration);
3558
}
3659

3760
private function make_cache_key($name) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace LaunchDarkly;
4+
5+
/**
6+
* Feature requester from an LDD-populated redis, with APCu caching.
7+
*
8+
* Unlike APC, APCu is actively maintained and is available from php53 to php7.
9+
*
10+
* @package LaunchDarkly
11+
*/
12+
class ApcuLDDFeatureRequester extends ApcLDDFeatureRequester
13+
{
14+
/**
15+
* @param $key
16+
* @param null $success
17+
* @return mixed
18+
*/
19+
protected function fetch($key, &$success = null)
20+
{
21+
return \apcu_fetch($key, $success);
22+
}
23+
24+
/**
25+
* @param $key
26+
* @param $var
27+
* @param int $ttl
28+
* @return bool
29+
*/
30+
protected function add($key, $var, $ttl = 0)
31+
{
32+
return \apcu_add($key, $var, $ttl);
33+
}
34+
}

src/LaunchDarkly/LDClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct($sdkKey, $options = array()) {
8888
$featureRequesterClass = '\\LaunchDarkly\\GuzzleFeatureRequester';
8989
}
9090

91-
if (!is_a($featureRequesterClass, FeatureRequester::class, true)) {
91+
if (!is_a($featureRequesterClass, '\LaunchDarkly\FeatureRequester', true)) {
9292
throw new \InvalidArgumentException;
9393
}
9494
$this->_featureRequester = new $featureRequesterClass($this->_baseUri, $sdkKey, $options);

0 commit comments

Comments
 (0)