Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ machine:
php:
version: 5.6.17
services:
- redis
- docker
dependencies:
pre:
- yes '' | pecl install -f apcu-4.0.10
- echo "extension=apcu.so" | sudo tee -a /opt/circleci/php/$(phpenv global)/etc/php.ini
- echo "apc.enable_cli = 1" | sudo tee -a /opt/circleci/php/$(phpenv global)/etc/php.ini
- docker pull php
- docker pull nyanpass/php5.5

test:
override:
- vendor/bin/phpunit tests --coverage-text
- ln -s vendor integration-tests/vendor
- vendor/bin/phpunit integration-tests/LDDFeatureRequesterTest.php

- composer update && vendor/bin/phpunit tests
- composer update --prefer-lowest && vendor/bin/phpunit tests

Expand Down
97 changes: 84 additions & 13 deletions integration-tests/LDDFeatureRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use LaunchDarkly\LDClient;
use LaunchDarkly\LDUserBuilder;
use Predis\Client;

class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase {

Expand All @@ -24,6 +25,9 @@ public function testGet() {
}

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

public function testGetApcu() {
if (!extension_loaded('apcu')) {
self::markTestSkipped('Install `apcu` extension to run this test.');
}

$redis = new Client([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379
]);

$client = new LDClient('BOGUS_API_KEY', [
'feature_requester_class' => '\LaunchDarkly\ApcuLDDFeatureRequester',
'apc_expiration' => 1
]);

$builder = new LDUserBuilder(3);
$user = $builder->build();

$redis->del('launchdarkly:features');
$this->assertEquals('alice', $client->variation('fiz', $user, 'alice'));
$redis->hset('launchdarkly:features', 'fiz', $this->gen_feature('fiz', 'buz'));
$this->assertEquals('buz', $client->variation('fiz', $user, 'alice'));

# cached value so not updated
$redis->hset('launchdarkly:features', 'fiz', $this->gen_feature('fiz', 'bob'));
$this->assertEquals('buz', $client->variation('fiz', $user, 'alice'));

\apcu_delete('launchdarkly:features.fiz');
$this->assertEquals('bob', $client->variation('fiz', $user, 'alice'));
}

private function gen_feature($key, $val) {
$data = <<<EOF
{"name": "Feature $key", "key": "$key", "kind": "flag", "salt": "Zm9v", "on": true,
"variations": [{"value": "$val", "weight": 100,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}},
{"value": false, "weight": 0,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}}],
"commitDate": "2015-09-08T21:24:16.712Z",
"creationDate": "2015-09-08T21:06:16.527Z",
"version": 4}
EOF;
return $data;
$data = [
'name' => 'Feature ' . $key,
'key' => $key,
'kind' => 'flag',
'salt' => 'Zm9v',
'on' => true,
'variations' => [
$val,
false,
],
'commitDate' => '2015-09-08T21:24:16.712Z',
'creationDate' => '2015-09-08T21:06:16.527Z',
'version' => 4,
'prerequisites' => [],
'targets' => [
[
'values' => [
$val,
],
'variation' => 0,
],
[
'values' => [
false,
],
'variation' => 1,
],
],
'rules' => [],
'fallthrough' => [
'rollout' => [
'variations' => [
[
'variation' => 0,
'weight' => 95000,
],
[
'variation' => 1,
'weight' => 5000,
],
],
],
],
'offVariation' => null,
'deleted' => false,
];

return \json_encode($data);
}

}
Expand Down
1 change: 1 addition & 0 deletions integration-tests/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/trusty64"

config.vm.provision :shell, path: "bootstrap.sh"
config.vm.provision :shell, path: "bootstrap.user.sh", privileged: false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
Expand Down
24 changes: 3 additions & 21 deletions integration-tests/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ set tabstop=4
EOF
chown vagrant.vagrant /home/vagrant/.vimrc

su - vagrant
cd ~vagrant
pwd
curl -s -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/bin/phpbrew
phpbrew init
phpbrew known --update
phpbrew update
phpbrew install 5.4.34 +default

echo "source $HOME/.phpbrew/bashrc" >> /home/vagrant/.bashrc
source $HOME/.bashrc
phpbrew switch php-5.4.34
phpbrew ext install apc
echo "apc.enable_cli = 1" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini


cd /home/vagrant/project/integration-tests
curl -sS https://getcomposer.org/installer | php
php composer.phar install
# enable APC for php5 CLI
echo "apc.enable_cli = 1" >> /etc/php5/cli/conf.d/20-apcu.ini
php -i | grep apc
31 changes: 31 additions & 0 deletions integration-tests/bootstrap.user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

Copy link
Contributor

@drichelson drichelson Feb 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing we have found useful in shell scripts is to add this line:
set -uxe
at line 2 which will do some handy things described here
(I'll add the lines once it is merged)

Copy link
Contributor Author

@abacaphiliac abacaphiliac Feb 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll add it right now in my drop-php54 branch lol i should really read the comment in its entirety before replying.

echo
echo "install phpbrew and php54"
cd ~
curl -s -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/bin/phpbrew
phpbrew init
phpbrew known --update
phpbrew update
phpbrew install 5.4.34 +default

echo
echo "switch php54"
echo "source $HOME/.phpbrew/bashrc" >> /home/vagrant/.bashrc
source $HOME/.phpbrew/bashrc
phpbrew switch 5.4.34

echo
echo "install php54-apc"
phpbrew ext install apc
echo "apc.enable_cli = 1" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini
php -i | grep apc
echo "date.timezone =UTC" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini

echo
echo "update project dependencies"
cd /home/vagrant/project/integration-tests
curl -sS https://getcomposer.org/installer | php
php composer.phar update
27 changes: 25 additions & 2 deletions src/LaunchDarkly/ApcLDDFeatureRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

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

/**
* @param $key
* @param $success
* @return mixed
*/
protected function fetch($key, &$success = null)
{
return \apc_fetch($key, $success);
}

protected function get_from_cache($key) {
$key = self::make_cache_key($key);
$enabled = apc_fetch($key);
$enabled = $this->fetch($key);
if ($enabled === false) {
return null;
}
Expand All @@ -30,8 +42,19 @@ protected function get_from_cache($key) {
}
}

/**
* @param $key
* @param $var
* @param int $ttl
* @return mixed
*/
protected function add($key, $var, $ttl = 0)
{
return \apc_add($key, $var, $ttl);
}

protected function store_in_cache($key, $val) {
apc_add($this->make_cache_key($key), $val, $this->_expiration);
$this->add($this->make_cache_key($key), $val, $this->_expiration);
}

private function make_cache_key($name) {
Expand Down
34 changes: 34 additions & 0 deletions src/LaunchDarkly/ApcuLDDFeatureRequester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace LaunchDarkly;

/**
* Feature requester from an LDD-populated redis, with APCu caching.
*
* Unlike APC, APCu is actively maintained and is available from php53 to php7.
*
* @package LaunchDarkly
*/
class ApcuLDDFeatureRequester extends ApcLDDFeatureRequester
{
/**
* @param $key
* @param null $success
* @return mixed
*/
protected function fetch($key, &$success = null)
{
return \apcu_fetch($key, $success);
}

/**
* @param $key
* @param $var
* @param int $ttl
* @return bool
*/
protected function add($key, $var, $ttl = 0)
{
return \apcu_add($key, $var, $ttl);
}
}
2 changes: 1 addition & 1 deletion src/LaunchDarkly/LDClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct($sdkKey, $options = array()) {
$featureRequesterClass = '\\LaunchDarkly\\GuzzleFeatureRequester';
}

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