public static function store(AppNotice $notice) { $binary_notification = $notice->serialize(); $config = Config::instance(); $tube = Config::instance()->queuePrefix() . self::SEP . $notice->getApp() . self::SEP . date('Ymd', Time::now()); $try = $config->retries() + 1; $conns = $config->connections(); $keys = array_keys($conns); shuffle($keys); $ttr = 30; // $config->ttr(); $priority = 0; //floor( $notice->getExpires() - time::now() / 3600 ); $delay = 0; foreach ($keys as $key) { $conn = $conns[$key]; if (!$try--) { break; } $res = $conn->putInTube($tube, $binary_notification, $priority, $delay, $ttr); if (!$res) { continue; } return $conn->hostInfo() . '-' . $res; } throw new Exception('storage error', $conns); }
public function replace($k, $v, $expires = 0) { if ($expires > Wrap::TTL_30_DAYS) { $expires -= Time::now(); } if ($this->core) { return $this->core->replace($k, $v, $expires); } if (!$this->get($k)) { return FALSE; } return $this->set($k, $v, $expires); }
public function flushOld($pattern = '*') { return $this->flush($pattern, NULL, Time::now() - Job::config()->ttl()); }
$e = NULL; try { $listing = $souk->buy($listing->id); } catch (Exception $e) { $e = $e->getMessage(); } Tap::is($e, 'bid only', 'when buying bid-only, fails'); DB\Transaction::reset(); DB\Connection::reset(); $listing = $souk->bid($listing->id, 1); Tap::is($listing->bid, 1, 'bid works fine, tho'); $listing = Souk($app, $seller_id)->auction(array('bid' => 0, 'reserve' => 5, 'item_id' => $item_id)); $listing = Souk($app, $buyer_id)->bid($listing->id, 5); Tap::is($listing->bid, 5, 'without enable_proxy, bid is set, not stepped'); $listing = Souk($app)->close($listing->id); Tap::is($listing->buyer, 0, 'when reserve isnt met, bidder doesnt win listing'); Tap::is($listing->closed, 1, 'even tho reserve wasnt met, closing still ends the bidding.'); unset($seller_id); unset($buyer_id); unset($item_id); Time::offset(86400 * 30); $id = 0; $ct = 0; while ($listings = Souk($app)->pending(0, 5, $id)) { foreach ($listings as $id) { $ct++; //Tap::debug('pending: ' . $id ); } } Tap::cmp_ok($ct, '>=', 1, "found at least 1 item in pending: {$ct} found"); //print "\n\n";
Tap::is(print_r($res, TRUE), print_r($expected, TRUE), 'search sort by low_price returns results sorted by price from low to high'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('sort' => 'high_price', 'item_id' => $item_id, 'seller' => $seller_id))) as $id => $listing) { $res[] = $listing->price; } $expected = $res; rsort($expected, SORT_NUMERIC); Tap::is(print_r($res, TRUE), print_r($expected, TRUE), 'search sort by high_price returns results sorted by price from high to low'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('sort' => 'expires_soon_delay', 'item_id' => $item_id, 'seller' => $seller_id))) as $id => $listing) { $res[] = $listing->expires; } $expected = $res; sort($expected, SORT_NUMERIC); Tap::is($res, $expected, 'search sort by expired_soon_delay sorted by expires'); Tap::cmp_ok(min($res), '>', Time::now() + 500, 'all of the results expire after now by more than 500 secs'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'floor' => 4, 'ceiling' => 6))) as $id => $listing) { $res[$listing->price] = TRUE; } $res = array_keys($res); sort($res); Tap::is($res, array(4, 5, 6), 'setting floor and ceiling limits the result set to prices in the correct range'); $search = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0)); $id = array_shift($search); souk($app)->close($id); $ids = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0)); Tap::ok(!in_array($id, $ids, TRUE), 'after closing an item, it no longer appears in the list of unclosed items'); $res = souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0, 'only' => 'bid'))); $bidonly = TRUE; foreach ($res as $listing) {
public static function currentShard() { return date('Ym', Time::now()); }
function advanceCurrentTime($secs = 1) { \Gaia\Time::offset($secs); }
protected function _isValid($k, $v) { if ($v === NULL) { return FALSE; } if (!is_array($v)) { $this->core->delete($k); return FALSE; } $ttl = $v[1]; if ($ttl == 0) { return TRUE; } if ($ttl < Time::now()) { $this->core->delete($k); return FALSE; } return TRUE; }
use Gaia\Time; if (!isset($extra_tests)) { $extra_tests = 0; } Tap::plan(27 + $extra_tests); Tap::ok($skein instanceof Skein\Iface, 'created new skein object, implements expected interface'); $id = $skein->add($data = array('foo' => mt_rand(1, 100000000))); Tap::ok(ctype_digit($id), 'added data, got back an id'); Tap::is($skein->get($id), $data, 'read back the data I stored'); Tap::is($skein->get(array($id)), array($id => $data), 'multi-get interface works too'); $data = array('foo' => mt_rand(1, 100000000)); $skein->store($id, $data); Tap::is($skein->get($id), $data, 'stored the data with new values, get returns what I wrote'); $batch = array($id => $data); for ($i = 0; $i < 10; $i++) { Time::offset(86400 * 5); $id = $skein->add($data = array('foo' => mt_rand(1, 100000000))); $batch[$id] = $data; } $ids = array_keys($batch); $res = $skein->get($ids); Tap::is($res, $batch, 'added a bunch of keys and read them back using get( ids ) interface'); Tap::is($skein->ids(array('limit' => 100)), $ids, 'got the keys back in ascending order'); Tap::is($res = $skein->ids(array('sort' => 'ascending', 'limit' => 5)), array_slice($ids, 0, 5), 'got the keys back in ascending order, limit 5'); Tap::is($res = $skein->ids(array('sort' => 'ascending', 'limit' => 5, 'start_after' => $ids[5])), array_slice($ids, 6, 5), 'got the keys back in ascending order, limit 5, starting after the 5th id'); Tap::is($res = $skein->ids(array('limit' => 1)), array($ids[0]), 'with limit 1, got back the 1st id'); $ids = array_reverse($ids); Tap::is($skein->ids(array('sort' => 'descending', 'limit' => 100)), $ids, 'got the keys back in descending order'); Tap::is($res = $skein->ids(array('sort' => 'descending', 'limit' => 5)), array_slice($ids, 0, 5), 'got the keys back in descending order, limit 5'); Tap::is($res = $skein->ids(array('sort' => 'descending', 'limit' => 5, 'start_after' => $ids[5])), array_slice($ids, 5, 5), 'got the keys back in descending order, limit 5, starting with the 5th id'); Tap::is($res = $skein->ids(array('sort' => 'descending', 'limit' => 1)), array($ids[0]), 'with limit 1, got back the last id');
/** * mark the job as failed */ public function fail() { if ($this->expires < Time::now()) { return $this->remove(); } if (!$this->id) { return FALSE; } list($server, $id) = explode('-', $this->id, 2); if (!$server) { return FALSE; } $conns = self::config()->connections(); if (!isset($conns[$server])) { return false; } $conn = $conns[$server]; $res = $conn->release(new \Pheanstalk_Job($id, ''), $this->priority, $this->ttr + 300); if (!$res) { throw new Exception('conn error', $conn); } return $res; }
Tap::ok($res, 'checked each key and got back what I wrote'); $ret = $cache->get(array_keys($data)); $res = TRUE; foreach ($data as $k => $v) { if ($ret[$k] != $v) { $res = FALSE; } } Tap::ok($res, 'grabbed the keys all at once, got what I wrote'); $k = 'gaia/cache/test/' . microtime(TRUE) . '/' . mt_rand(1, 10000); Tap::ok($cache->add($k, 1, 10), 'adding a non-existent key'); Tap::ok(!$cache->add($k, 1, 10), 'second time, the add fails'); if ($skip_expiration_tests || !method_exists($cache, 'ttlEnabled') || !$cache->ttlEnabled()) { Tap::ok(TRUE, 'skipping expire test'); } else { Time::offset(11); Tap::ok($cache->add($k, 1, 10), 'after expiration time, add works'); } Tap::ok($cache->replace($k, 1, 10), 'replace works after the successful add'); Tap::ok($cache->delete($k), 'successfully deleted the key'); Tap::ok(!$cache->replace($k, 1, 10), 'replace fails after key deletion'); Tap::ok($cache->add($k, 1, 10), 'add works after key deletion'); Tap::ok($cache->replace($k, 1, 10), 'replace works after key is added'); $k = 'gaia/cache/test/' . microtime(TRUE) . '/' . mt_rand(1, 10000); Tap::ok($cache->get($k) === NULL, 'cache get on a non-existent key returns NULL (not false)'); $k = 'gaia/cache/test/' . microtime(TRUE) . '/' . mt_rand(1, 10000); Tap::is($cache->increment($k, 1), FALSE, 'increment a new key returns (bool) FALSE'); Tap::is($cache->decrement($k, 1), FALSE, 'decrement a new key returns (bool) FALSE'); Tap::cmp_ok($cache->set($k, 'test'), '===', TRUE, 'setting a key returns bool TRUE'); Tap::cmp_ok($cache->replace($k, 'test1'), '===', TRUE, 'replacing a key returns bool TRUE'); Tap::cmp_ok($cache->{$k} = '11', '===', '11', 'setting using the magic method property approach returns value');
protected function calculateShort() { list($value, $time) = $this->storage; $elapsed_time = Time::now() - $time; if ($elapsed_time < $this->options->short) { return $this->options->short - $elapsed_time; } return 0; }
} if (!isset($buyer_id)) { $buyer_id = uniqueUserId(); } if (!isset($item_id)) { $item_id = uniqueNumber(1, 100000); } Transaction::reset(); Connection::reset(); Transaction::start(); $souk = souk($app, $seller_id); $listing = $souk->auction(array('price' => 10, 'bid' => 0, 'item_id' => $item_id)); $read = $souk->get($listing->id); Tap::is($listing, $read, 'new auction is readable before transaction is committed, while in the transaction'); Transaction::rollback(); Transaction::reset(); Connection::reset(); $read = $souk->get($listing->id); Tap::is($read, NULL, 'after transaction rollback, no entry found'); Transaction::reset(); Transaction::start(); $souk = souk($app, $seller_id); $listing = $souk->auction(array('price' => 10, 'bid' => 0, 'item_id' => $item_id)); Time::offset(86400 * 15); $listing = $souk->close($listing->id); Transaction::commit(); Tap::is($listing->closed, 1, 'creating and closing a listing works inside a transaction'); unset($seller_id); unset($buyer_id); unset($item_id); //print "\n\n";
protected static function time() { return Time::now(); }
protected function ttl($ttl) { if ($ttl < 1) { return '4294967295'; } elseif ($ttl < Time::now() + Wrap::TTL_30_DAYS) { return Time::now() + $ttl; } }
public function flushOld($pattern = '*') { $days = (int) Config::instance()->get('expired_days'); if ($days < 1) { $days = 1; } $ttl = 86400 * $days; return $this->flush($pattern, NULL, Time::now() - $ttl); }
/** * returns the current time. */ public static function now() { return \Gaia\Time::now(); }
function replace($k, $v, $expire = 0) { if (!$this->core->replace($k . '/__lock/', 1, $expire)) { return FALSE; } if (!$expire) { $expire = self::DEFAULT_TTL; } $this->core->set($k . '/__exp/', Time::now() + $expire); return $this->core->set($k, $v); }