public function testSynchronized() { $a = array(); self::$db->synchronized('sync', function () use(&$a) { $a[] = 'called'; self::$db->eachId(function ($id) { if ($id == '_sync_lock') { $a[] = 'each'; } }); $file = self::$db->getPath() . '_sync_lock'; $handle = fopen($file, 'w+'); if ($handle && flock($handle, LOCK_EX | LOCK_NB, $wouldblock)) { flock($handle, LOCK_UN); fclose($handle); } else { $a[] = 'locked'; } if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // this fails on windows 8 // whereas LOCK_NB appears to be supported, at least by windows 8 // $this->assertEquals(1, $wouldblock); } else { $this->assertEquals(1, $wouldblock); } self::$db->synchronized('sync', function () use(&$a) { $a[] = 'nested'; }); }); $ex = array('called', 'locked', 'nested'); $this->assertEquals($ex, $a); }
/** * Apply a synchronized operation on the index * * @param callable $func * @throws Exception */ public function apply($func) { $self = $this; $this->db->synchronized($this->name . '_index', function () use($self, $func) { $self->restore(); $func(); }); }