Skip to content

Commit 67c6bbb

Browse files
alexp8Sneer-ra2
andauthored
fix seconds in queue (#460)
* add more logs and update secondsInQueue (#456) Co-authored-by: Sneer-ra2 <[email protected]> * set AutoSaveInterval to 0 * update secondsInQueue to use updated_at * fix secondsInQueue bug --------- Co-authored-by: Sneer-ra2 <[email protected]>
1 parent 3987b76 commit 67c6bbb

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

cncnet-api/app/Models/QmQueueEntry.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function ladderHistory()
1919

2020
public function secondsinQueue()
2121
{
22-
// Calculate the difference between updated_at and created_at
23-
// updated_at is touched by matchup handlers when no match is found
24-
return $this->updated_at->diffInSeconds($this->created_at);
22+
// Calculate the difference from created_at to updated_at
23+
// updated_at is touched by FindOpponentJob when matching starts
24+
return $this->created_at->diffInSeconds($this->updated_at);
2525
}
2626
}

cncnet-api/phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
processIsolation="false"
66
stopOnFailure="false">
77
<testsuites>
8+
<testsuite name="Unit">
9+
<directory suffix="Test.php">./tests/Unit</directory>
10+
</testsuite>
811
<testsuite name="Feature">
912
<directory suffix="Test.php">./tests/Feature</directory>
1013
</testsuite>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Tests\Unit\Models;
4+
5+
use App\Models\QmQueueEntry;
6+
use Carbon\Carbon;
7+
use Tests\TestCase;
8+
9+
class QmQueueEntryTest extends TestCase
10+
{
11+
/**
12+
* Test that secondsinQueue() returns a positive value when updated_at is after created_at.
13+
* This verifies the bug fix where the parameter order was reversed.
14+
*/
15+
public function test_seconds_in_queue_returns_positive_when_updated_after_created(): void
16+
{
17+
// Create a QmQueueEntry instance without saving to database
18+
$entry = new QmQueueEntry();
19+
20+
// Set created_at to an earlier time
21+
$entry->created_at = Carbon::parse('2025-12-19 23:57:08');
22+
23+
// Set updated_at to a later time (simulating touch() being called)
24+
$entry->updated_at = Carbon::parse('2025-12-19 23:57:11');
25+
26+
// Calculate expected difference (3 seconds)
27+
$expectedSeconds = 3;
28+
29+
// Assert that secondsinQueue() returns the correct positive value
30+
$this->assertEquals($expectedSeconds, $entry->secondsinQueue());
31+
32+
// Assert that the result is positive
33+
$this->assertGreaterThan(0, $entry->secondsinQueue());
34+
}
35+
36+
/**
37+
* Test that secondsinQueue() calculates the correct time difference.
38+
*/
39+
public function test_seconds_in_queue_calculates_correct_time_difference(): void
40+
{
41+
$entry = new QmQueueEntry();
42+
43+
// Entry created at 00:12:20
44+
$entry->created_at = Carbon::parse('2025-12-20 00:12:20');
45+
46+
// Updated 397 seconds later at 00:18:57
47+
$entry->updated_at = Carbon::parse('2025-12-20 00:18:57');
48+
49+
// Should return 397 seconds
50+
$this->assertEquals(397, $entry->secondsinQueue());
51+
}
52+
53+
/**
54+
* Test that secondsinQueue() returns 0 when created_at equals updated_at.
55+
* This happens when a queue entry is first created.
56+
*/
57+
public function test_seconds_in_queue_returns_zero_when_timestamps_equal(): void
58+
{
59+
$entry = new QmQueueEntry();
60+
61+
$timestamp = Carbon::parse('2025-12-19 23:57:08');
62+
$entry->created_at = $timestamp;
63+
$entry->updated_at = $timestamp;
64+
65+
$this->assertEquals(0, $entry->secondsinQueue());
66+
}
67+
68+
/**
69+
* Test that secondsinQueue() with longer wait times.
70+
* This verifies the calculation works for realistic queue times.
71+
*/
72+
public function test_seconds_in_queue_with_longer_wait_time(): void
73+
{
74+
$entry = new QmQueueEntry();
75+
76+
// Entry created
77+
$entry->created_at = Carbon::parse('2025-12-19 23:00:00');
78+
79+
// 5 minutes later (300 seconds)
80+
$entry->updated_at = Carbon::parse('2025-12-19 23:05:00');
81+
82+
$this->assertEquals(300, $entry->secondsinQueue());
83+
$this->assertGreaterThan(0, $entry->secondsinQueue());
84+
}
85+
86+
/**
87+
* Regression test: Verify the old buggy implementation would have failed.
88+
* This documents what the bug was and ensures it doesn't come back.
89+
*/
90+
public function test_parameter_order_matters(): void
91+
{
92+
$entry = new QmQueueEntry();
93+
94+
$entry->created_at = Carbon::parse('2025-12-19 23:57:08');
95+
$entry->updated_at = Carbon::parse('2025-12-19 23:57:11');
96+
97+
// The CORRECT implementation: created_at->diffInSeconds(updated_at)
98+
$correct = $entry->created_at->diffInSeconds($entry->updated_at);
99+
100+
// The BUGGY implementation: updated_at->diffInSeconds(created_at)
101+
// This would return a negative value with the signed parameter
102+
$buggy = $entry->updated_at->diffInSeconds($entry->created_at, false);
103+
104+
// Assert that the correct implementation is positive
105+
$this->assertGreaterThan(0, $correct);
106+
$this->assertEquals(3, $correct);
107+
108+
// Assert that the buggy implementation was negative
109+
$this->assertLessThan(0, $buggy);
110+
$this->assertEquals(-3, $buggy);
111+
112+
// Assert that secondsinQueue() uses the correct implementation
113+
$this->assertEquals($correct, $entry->secondsinQueue());
114+
}
115+
}

0 commit comments

Comments
 (0)