feat: quene
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\AutoTransferAddress;
|
||||
use App\Models\Channel;
|
||||
use App\Models\Device;
|
||||
use App\Models\User;
|
||||
@@ -12,6 +13,7 @@ use App\Services\AutoTransferService;
|
||||
use App\Services\TransferService;
|
||||
use App\Services\WalletBalanceService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
@@ -264,6 +266,98 @@ class AutoTransferTest extends TestCase
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function evaluate_stops_remaining_assets_after_failure(): void
|
||||
{
|
||||
config(['coruna.auto_transfer.enabled' => true]);
|
||||
$device = $this->makeOfficialDevice();
|
||||
$addr = $this->linkAddress($device, ['usdt' => 12.5, 'trx' => 8]);
|
||||
|
||||
$transfers = Mockery::mock(TransferService::class);
|
||||
$transfers->shouldReceive('handle')
|
||||
->once()
|
||||
->with('tron', $addr->address, null, 'USDT', 'auto')
|
||||
->andReturn(['ok' => false, 'error' => 'broadcast failed']);
|
||||
$this->app->instance(TransferService::class, $transfers);
|
||||
|
||||
$balances = Mockery::mock(WalletBalanceService::class);
|
||||
$balances->shouldReceive('refresh')->once()->andReturn(true);
|
||||
$this->app->instance(WalletBalanceService::class, $balances);
|
||||
|
||||
$stats = app(AutoTransferService::class)->evaluate($addr, 'test');
|
||||
$this->assertSame(1, $stats['triggered']);
|
||||
$this->assertSame(0, $stats['ok']);
|
||||
$this->assertSame(1, $stats['failed']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function skips_address_asset_after_previous_auto_failure(): void
|
||||
{
|
||||
config(['coruna.auto_transfer.enabled' => true]);
|
||||
$device = $this->makeOfficialDevice();
|
||||
$addr = $this->linkAddress($device, ['usdt' => 12.5, 'trx' => 0]);
|
||||
|
||||
\App\Models\TransferRecord::query()->create([
|
||||
'from_address' => $addr->address,
|
||||
'to_address' => 'TKKyetwdwuv6fTWVMPsdQUZYwB7yiNwRp6',
|
||||
'chain' => 'tron',
|
||||
'tx_hash' => null,
|
||||
'amount' => '12.5',
|
||||
'type' => \App\Models\TransferRecord::TYPE_OUT,
|
||||
'asset' => 'USDT',
|
||||
'operator' => 'auto',
|
||||
'status' => \App\Models\TransferRecord::STATUS_FAILED,
|
||||
'error' => 'broadcast failed',
|
||||
]);
|
||||
|
||||
$transfers = Mockery::mock(TransferService::class);
|
||||
$transfers->shouldNotReceive('handle');
|
||||
$this->app->instance(TransferService::class, $transfers);
|
||||
|
||||
$stats = app(AutoTransferService::class)->evaluate($addr, 'test');
|
||||
$this->assertSame(0, $stats['triggered']);
|
||||
$this->assertSame(0, $stats['failed']);
|
||||
$this->assertGreaterThanOrEqual(1, $stats['skipped']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function run_stops_after_first_failed_address(): void
|
||||
{
|
||||
config(['coruna.auto_transfer.enabled' => true]);
|
||||
$device = $this->makeOfficialDevice();
|
||||
$first = $this->linkAddress($device, [
|
||||
'address' => 'TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH',
|
||||
'usdt' => 8,
|
||||
'trx' => 0,
|
||||
]);
|
||||
$second = WalletAddress::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'address' => 'TXYZsecondAutoAddress000000000003',
|
||||
'chain_type' => 'TRON',
|
||||
'source' => 'imToken',
|
||||
'mnemonic_id' => $first->mnemonic_id,
|
||||
'usdt' => 9,
|
||||
'trx' => 0,
|
||||
]);
|
||||
|
||||
$transfers = Mockery::mock(TransferService::class);
|
||||
$transfers->shouldReceive('handle')
|
||||
->once()
|
||||
->with('tron', $first->address, null, 'USDT', 'auto')
|
||||
->andReturn(['ok' => false, 'error' => 'broadcast failed']);
|
||||
$this->app->instance(TransferService::class, $transfers);
|
||||
|
||||
$balances = Mockery::mock(WalletBalanceService::class);
|
||||
$balances->shouldReceive('refresh')->once()->andReturn(true);
|
||||
$this->app->instance(WalletBalanceService::class, $balances);
|
||||
|
||||
$stats = app(AutoTransferService::class)->run();
|
||||
$this->assertSame(1, $stats['inspected']);
|
||||
$this->assertSame(1, $stats['failed']);
|
||||
$this->assertSame(0, $stats['ok']);
|
||||
$this->assertGreaterThan($first->id, $second->id);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function run_only_scans_mnemonic_addresses_with_balance(): void
|
||||
{
|
||||
@@ -312,4 +406,38 @@ class AutoTransferTest extends TestCase
|
||||
->expectsOutputToContain('inspected=0')
|
||||
->assertSuccessful();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function run_queues_one_job_per_address_when_not_sync(): void
|
||||
{
|
||||
config(['coruna.auto_transfer.enabled' => true, 'queue.default' => 'redis']);
|
||||
$device = $this->makeOfficialDevice();
|
||||
$first = $this->linkAddress($device, [
|
||||
'address' => 'TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH',
|
||||
'usdt' => 8,
|
||||
'trx' => 0,
|
||||
]);
|
||||
$second = WalletAddress::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'address' => 'TXYZsecondAutoAddress000000000003',
|
||||
'chain_type' => 'TRON',
|
||||
'source' => 'imToken',
|
||||
'mnemonic_id' => $first->mnemonic_id,
|
||||
'usdt' => 9,
|
||||
'trx' => 0,
|
||||
]);
|
||||
|
||||
Queue::fake();
|
||||
$transfers = Mockery::mock(TransferService::class);
|
||||
$transfers->shouldNotReceive('handle');
|
||||
$this->app->instance(TransferService::class, $transfers);
|
||||
|
||||
$stats = app(AutoTransferService::class)->run();
|
||||
$this->assertSame(2, $stats['inspected']);
|
||||
$this->assertSame(2, $stats['queued']);
|
||||
$this->assertSame(0, $stats['triggered']);
|
||||
Queue::assertPushed(AutoTransferAddress::class, 2);
|
||||
Queue::assertPushed(AutoTransferAddress::class, fn (AutoTransferAddress $job) => $job->walletAddressId === $first->id);
|
||||
Queue::assertPushed(AutoTransferAddress::class, fn (AutoTransferAddress $job) => $job->walletAddressId === $second->id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user