Skip to content

Commit

Permalink
完善测试用例,修复一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 26, 2023
1 parent 7178665 commit 2623848
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ public function __construct(protected IConnectionLoadBalancer $connectionLoadBal
{
}

public function setConnectionLoadBalancer(IConnectionLoadBalancer $connectionLoadBalancer): self
{
$this->connectionLoadBalancer = $connectionLoadBalancer;

return $this;
}

public function getConnectionLoadBalancer(): IConnectionLoadBalancer
{
return $this->connectionLoadBalancer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public static function createConnectionConfig(string|array $config): IConnection

public function __construct(IConnectionLoadBalancer $connectionLoadBalancer);

/**
* 设置连接负载均衡器.
*/
public function setConnectionLoadBalancer(IConnectionLoadBalancer $connectionLoadBalancer): self;

/**
* 获取连接负载均衡器.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
interface IConnectionLoadBalancer
{
/**
* @param IConnectionConfig[] $configs
*/
public function __construct(array $configs);

/**
* @param IConnectionConfig[] $configs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function changeTotalConnectionCount(int $quantity): int
return $this->totalConnectionCount += $quantity;
}

/**
* @codeCoverageIgnore
*/
public function changeFreeConnectionCount(int $quantity): int
{
return $this->freeConnectionCount += $quantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function releaseConnection(IConnection $connection): void
{
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
if (ConnectionStatus::WaitRelease !== $connection->getStatus())
{
Expand All @@ -89,6 +89,9 @@ public function releaseConnection(IConnection $connection): void
}
}

/**
* @codeCoverageIgnore
*/
public function detachConnection(IConnection $connection): void
{
// 本管理器创建的连接本来就是分离的,无需处理
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __destruct()
$id = (string) spl_object_id($this);
if (ChannelContainer::hasChannel($id))
{
ChannelContainer::removeChannel($id);
ChannelContainer::removeChannel($id); // @codeCoverageIgnore
}
}

Expand All @@ -65,11 +65,6 @@ public function getConnection(): ?\WeakReference
return $this->connection;
}

public function hasConnection(): bool
{
return $this->connection && $this->connection->get();
}

public function lock(float $timeout = 0): bool
{
if ($this->isFree || ChannelContainer::pop((string) spl_object_id($this), $timeout))
Expand All @@ -80,7 +75,7 @@ public function lock(float $timeout = 0): bool
return true;
}

return false;
return false; // @codeCoverageIgnore
}

public function release(): void
Expand All @@ -90,11 +85,13 @@ public function release(): void
$id = (string) spl_object_id($this);
if (ChannelContainer::hasChannel($id))
{
// @codeCoverageIgnoreStart
$channel = ChannelContainer::getChannel($id);
if (($channel->stats()['consumer_num'] ?? 0) > 0)
{
$channel->push(true);
}
// @codeCoverageIgnoreEnd
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function getConnection(): IConnection
}
/** @var InstanceResource|false $resource */
$resource = $this->queue->pop($waitTimeout);
// @codeCoverageIgnoreStart
if (!$resource)
{
if (\SWOOLE_CHANNEL_TIMEOUT === $this->queue->errCode)
Expand All @@ -151,6 +152,7 @@ public function getConnection(): IConnection
{
throw new \RuntimeException('Pool lock resource failed');
}
// @codeCoverageIgnoreEnd
$driver = $this->getDriver();
if ($config->isCheckStateWhenGetResource() && !$driver->checkAvailable($instance = $resource->getInstance()))
{
Expand All @@ -161,11 +163,13 @@ public function getConnection(): IConnection
$resource->setInstance($instance);
$this->instanceMap[$instance] = $resource;
}
// @codeCoverageIgnoreStart
catch (\Throwable $th)
{
$this->removeResource($resource);
throw $th;
}
// @codeCoverageIgnoreEnd
}

$connection = new Connection($this, $resource->getInstance());
Expand All @@ -184,7 +188,7 @@ public function releaseConnection(IConnection $connection): void
{
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
if (ConnectionStatus::WaitRelease !== $connection->getStatus())
{
Expand Down Expand Up @@ -217,16 +221,16 @@ public function detachConnection(IConnection $connection): void
{
if (!$this->available)
{
throw new \RuntimeException('Connection manager is unavailable');
throw new \RuntimeException('Connection manager is unavailable'); // @codeCoverageIgnore
}
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
$instance = $connection->getInstance();
if (!isset($this->instanceMap[$instance]))
{
throw new \RuntimeException('Connection is not in this connection manager');
throw new \RuntimeException('Connection is not in this connection manager'); // @codeCoverageIgnore
}
$resource = $this->instanceMap[$instance];
$this->resources->detach($resource);
Expand Down Expand Up @@ -301,7 +305,10 @@ protected function addResource(): InstanceResource
}
}

public function removeResource(InstanceResource $resource, bool $buildQueue = false): void
/**
* @codeCoverageIgnore
*/
protected function removeResource(InstanceResource $resource, bool $buildQueue = false): void
{
$this->resources->detach($resource);
if ($buildQueue)
Expand Down Expand Up @@ -331,7 +338,7 @@ protected function buildQueue(): void
/**
* {@inheritDoc}
*/
public function gc(): void
protected function gc(): void
{
$hasGC = false;
$poolConfig = $this->config->getPool();
Expand Down Expand Up @@ -368,7 +375,7 @@ public function gc(): void
/**
* 开始自动垃圾回收.
*/
public function startAutoGC(): void
protected function startAutoGC(): void
{
$gcInterval = $this->config->getPool()->getGCInterval();
if ($gcInterval > 0)
Expand All @@ -381,7 +388,7 @@ public function startAutoGC(): void
/**
* 停止自动垃圾回收.
*/
public function stopAutoGC(): void
protected function stopAutoGC(): void
{
if (null !== $this->gcTimerId)
{
Expand All @@ -396,7 +403,7 @@ public function stopAutoGC(): void
/**
* 心跳.
*/
public function heartbeat(): void
protected function heartbeat(): void
{
if ($this->heartbeatRunning)
{
Expand All @@ -413,7 +420,7 @@ public function heartbeat(): void
$driver ??= $this->getDriver();
try
{
$available = $driver->checkAvailable($resource->getInstance());
$available = $driver->ping($resource->getInstance());
}
catch (\Throwable $th)
{
Expand Down Expand Up @@ -449,7 +456,7 @@ public function heartbeat(): void
/**
* 开始心跳维持资源.
*/
public function startHeartbeat(): void
protected function startHeartbeat(): void
{
if (null !== ($heartbeatInterval = $this->config->getPool()->getHeartbeatInterval()))
{
Expand All @@ -461,7 +468,7 @@ public function startHeartbeat(): void
/**
* 停止心跳维持资源.
*/
public function stopHeartbeat(): void
protected function stopHeartbeat(): void
{
if (null !== $this->heartbeatTimerId)
{
Expand All @@ -474,12 +481,12 @@ public function stopHeartbeat(): void
}
}

public function getFree(): int
protected function getFree(): int
{
return $this->queue->length();
}

public function getCount(): int
protected function getCount(): int
{
return \count($this->resources) + $this->addingResources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,10 @@ public function __construct(?string $driver = null, ?bool $enableStatistics = nu
{
$poolConfig['maxIdleTime'] = $config['pool']['maxIdleTime'];
}
if (isset($config['pool']['requestResourceCheckInterval']))
{
$poolConfig['requestResourceCheckInterval'] = $config['pool']['requestResourceCheckInterval'];
}
if (isset($config['pool']['heartbeatInterval']))
{
$poolConfig['heartbeatInterval'] = $config['pool']['heartbeatInterval'];
}
if (isset($config['pool']['checkStateWhenGetResource']))
{
$poolConfig['checkStateWhenGetResource'] = $config['pool']['checkStateWhenGetResource'];
}
}
$this->pool = new PoolConfig(...$poolConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getConnection(): IConnection
{
if (!$this->available)
{
throw new \RuntimeException('Connection manager is unavailable');
throw new \RuntimeException('Connection manager is unavailable'); // @codeCoverageIgnore
}

return RequestContext::use(function (ContextData $context) {
Expand Down Expand Up @@ -97,18 +97,22 @@ public function getConnection(): IConnection
$context[static::class][$this->id] = $instance = $driver->connect($instance);
$this->instanceMap[$instance] = $connectionData;
}
// @codeCoverageIgnoreStart
catch (\Throwable $th)
{
unset($context[static::class][$this->id]);
throw $th;
}
// @codeCoverageIgnoreEnd
}
$connectionData = $this->instanceMap[$instance];
$connection = $connectionData->getConnection()->get();
if (!$connection || ConnectionStatus::Available !== $connection->getStatus())
{
// @codeCoverageIgnoreStart
$connection = new Connection($this, $instance);
$connectionData->setConnection($connection);
// @codeCoverageIgnoreEnd
}
}
else
Expand Down Expand Up @@ -145,7 +149,7 @@ public function releaseConnection(IConnection $connection): void
{
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
if (ConnectionStatus::WaitRelease !== $connection->getStatus())
{
Expand Down Expand Up @@ -183,16 +187,16 @@ public function detachConnection(IConnection $connection): void
{
if (!$this->available)
{
throw new \RuntimeException('Connection manager is unavailable');
throw new \RuntimeException('Connection manager is unavailable'); // @codeCoverageIgnore
}
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
$instance = $connection->getInstance();
if (!isset($this->instanceMap[$instance]))
{
throw new \RuntimeException('Connection is not in this connection manager');
throw new \RuntimeException('Connection is not in this connection manager'); // @codeCoverageIgnore
}
$connectionData = $this->instanceMap[$instance];
$context = RequestContext::getInstance()->get($connectionData->getContextFlag());
Expand All @@ -216,7 +220,7 @@ protected function __close(): void
}
else
{
$this->getDriver()->close($instance);
$this->getDriver()->close($instance); // @codeCoverageIgnore
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ public function getConnection(): IConnection
try
{
$driver->close($instance);
$instance = $driver->connect($instance);
$this->connection = new Connection($this, $instance);
$newInstance = $driver->connect($instance);
if ($instance !== $newInstance)
{
$this->connection = new Connection($this, $newInstance);
}
}
// @codeCoverageIgnoreStart
catch (\Throwable $th)
{
$this->connection = null;
throw $th;
}
// @codeCoverageIgnoreEnd
}
}
if ($enableStatistics)
Expand All @@ -102,7 +107,7 @@ public function releaseConnection(IConnection $connection): void
{
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
if (ConnectionStatus::WaitRelease !== $connection->getStatus())
{
Expand Down Expand Up @@ -130,15 +135,15 @@ public function detachConnection(IConnection $connection): void
{
if (!$this->available)
{
throw new \RuntimeException('Connection manager is unavailable');
throw new \RuntimeException('Connection manager is unavailable'); // @codeCoverageIgnore
}
if ($connection->getManager() !== $this)
{
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class));
throw new \RuntimeException(sprintf('Connection manager %s cannot release connection, because the connection manager of this connection is %s', static::class, $connection->getManager()::class)); // @codeCoverageIgnore
}
if ($this->connection !== $connection)
{
throw new \RuntimeException('Connection is not in this connection manager');
throw new \RuntimeException('Connection is not in this connection manager'); // @codeCoverageIgnore
}
$this->connection = null;
}
Expand Down
Loading

0 comments on commit 2623848

Please sign in to comment.