public function testRoundRobin() { // given $expected = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002)); $stubResolver = StubResolver::createDoubleResult(); // when $lb = new ConsulLoadBalancer($stubResolver, new RoundRobinLoadBalancerStrategy(), "foo"); $foundA = array($lb->next(), $lb->next()); $foundB = array($lb->next(), $lb->next()); // then // round robin behavior $this->assertEquals($foundA[0]->getHost(), $foundB[0]->getHost()); $this->assertEquals($foundA[0]->getPort(), $foundB[0]->getPort()); $this->assertEquals($foundA[1]->getHost(), $foundB[1]->getHost()); $this->assertEquals($foundA[1]->getPort(), $foundB[1]->getPort()); // sort by target since order isn't deterministic usort($foundA, function ($a, $b) { return strcmp($a->getHost(), $b->getHost()); }); // matches expected $this->assertEquals($expected[0]->getHost(), $foundA[0]->getHost()); $this->assertEquals($expected[0]->getPort(), $foundA[0]->getPort()); $this->assertEquals($expected[1]->getHost(), $foundA[1]->getHost()); $this->assertEquals($expected[1]->getPort(), $foundA[1]->getPort()); }
public function resolve($name, $type) { $record = dns_get_record($name, $type); $mapped = array(); foreach ($record as $address) { $mapped[] = Url::fromHostAndPort($address['target'], $address['port']); } return $mapped; }
public function testLoadBalancerLookup() { // given $expected = Url::fromHostAndPort("foo1.fliglio.com", 8001); $stubResolver = StubResolver::createSingleResult(); // when $lb = new ConsulLoadBalancer($stubResolver, new RoundRobinLoadBalancerStrategy(), "foo"); $found = $lb->next(); // then $this->assertEquals($expected->getHost(), $found->getHost()); $this->assertEquals($expected->getPort(), $found->getPort()); }
public function testAddressProviderFactory() { // given $expected = Url::fromHostAndPort("foo1.fliglio.com", 8001); $stubResolver = StubResolver::createSingleResult(); // when $fac = new AddressProviderFactory($stubResolver); $ap = $fac->createConsulAddressProvider("foo"); $found = $ap->getAddress(); // then $this->assertEquals($expected->getHost(), $found->getHost()); $this->assertEquals($expected->getPort(), $found->getPort()); }
public function testSRVLookup() { // given $expected = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002)); // when $resv = new DnsResolver(); $found = $resv->resolve("foo.service.fliglio.com", DNS_SRV); // sort by target since order isn't deterministic usort($found, function ($a, $b) { return strcmp($a->getHost(), $b->getHost()); }); // then $this->assertEquals($expected[0]->getHost(), $found[0]->getHost()); $this->assertEquals($expected[1]->getHost(), $found[1]->getHost()); $this->assertEquals(count($expected), count($found)); }
public static function createDoubleResult() { $stubSrv = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002)); return new self($stubSrv); }