5 - Integrate buffer multiplier into pipeline allocation service
This commit is contained in:
parent
772f4c1c5a
commit
50b7caf1f9
2 changed files with 71 additions and 1 deletions
|
|
@ -81,7 +81,7 @@ private function calculateBucketAllocation(Bucket $bucket, int $remainingAmount)
|
||||||
*/
|
*/
|
||||||
private function calculateFixedAllocation(Bucket $bucket, int $remainingAmount): int
|
private function calculateFixedAllocation(Bucket $bucket, int $remainingAmount): int
|
||||||
{
|
{
|
||||||
$bucketCapacity = (int) ($bucket->allocation_value ?? 0);
|
$bucketCapacity = (int) round($bucket->getEffectiveCapacity());
|
||||||
$currentBalance = $bucket->getCurrentBalance();
|
$currentBalance = $bucket->getCurrentBalance();
|
||||||
$availableSpace = max(0, $bucketCapacity - $currentBalance);
|
$availableSpace = max(0, $bucketCapacity - $currentBalance);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -339,4 +339,74 @@ public function test_percentage_allocation_with_insufficient_remaining_amount()
|
||||||
$this->assertEquals(95000, $draws[0]->amount);
|
$this->assertEquals(95000, $draws[0]->amount);
|
||||||
$this->assertEquals(1000, $draws[1]->amount); // 20% of $50
|
$this->assertEquals(1000, $draws[1]->amount); // 20% of $50
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_fixed_allocation_respects_buffer_multiplier(): void
|
||||||
|
{
|
||||||
|
Bucket::factory()->create([
|
||||||
|
'scenario_id' => $this->scenario->id,
|
||||||
|
'allocation_type' => BucketAllocationTypeEnum::FIXED_LIMIT,
|
||||||
|
'allocation_value' => 50000,
|
||||||
|
'buffer_multiplier' => 1.0,
|
||||||
|
'starting_amount' => 0,
|
||||||
|
'priority' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Effective capacity = 50000 * (1 + 1.0) = 100000
|
||||||
|
$draws = $this->service->allocateInflow($this->scenario, 100000);
|
||||||
|
|
||||||
|
$this->assertCount(1, $draws);
|
||||||
|
$this->assertEquals(100000, $draws[0]->amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_fixed_allocation_with_buffer_partially_filled(): void
|
||||||
|
{
|
||||||
|
$bucket = Bucket::factory()->create([
|
||||||
|
'scenario_id' => $this->scenario->id,
|
||||||
|
'allocation_type' => BucketAllocationTypeEnum::FIXED_LIMIT,
|
||||||
|
'allocation_value' => 50000,
|
||||||
|
'buffer_multiplier' => 1.0,
|
||||||
|
'starting_amount' => 30000,
|
||||||
|
'priority' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Effective capacity = 100000, balance = 30000, available = 70000
|
||||||
|
$draws = $this->service->allocateInflow($this->scenario, 80000);
|
||||||
|
|
||||||
|
$this->assertCount(1, $draws);
|
||||||
|
$this->assertEquals(70000, $draws[0]->amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_buffer_does_not_affect_percentage_allocation(): void
|
||||||
|
{
|
||||||
|
Bucket::factory()->create([
|
||||||
|
'scenario_id' => $this->scenario->id,
|
||||||
|
'allocation_type' => BucketAllocationTypeEnum::PERCENTAGE,
|
||||||
|
'allocation_value' => 25.00,
|
||||||
|
'buffer_multiplier' => 1.0,
|
||||||
|
'starting_amount' => 0,
|
||||||
|
'priority' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$draws = $this->service->allocateInflow($this->scenario, 100000);
|
||||||
|
|
||||||
|
$this->assertCount(1, $draws);
|
||||||
|
$this->assertEquals(25000, $draws[0]->amount); // 25% of 100000
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_buffer_does_not_affect_unlimited_allocation(): void
|
||||||
|
{
|
||||||
|
Bucket::factory()->create([
|
||||||
|
'scenario_id' => $this->scenario->id,
|
||||||
|
'allocation_type' => BucketAllocationTypeEnum::UNLIMITED,
|
||||||
|
'allocation_value' => null,
|
||||||
|
'buffer_multiplier' => 1.0,
|
||||||
|
'starting_amount' => 0,
|
||||||
|
'priority' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$draws = $this->service->allocateInflow($this->scenario, 100000);
|
||||||
|
|
||||||
|
$this->assertCount(1, $draws);
|
||||||
|
$this->assertEquals(100000, $draws[0]->amount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue