4
4
"context"
5
5
"database/sql"
6
6
"errors"
7
- "sync"
8
7
"testing"
9
8
"time"
10
9
@@ -1724,34 +1723,27 @@ func TestExecutorAutostartSkipsWhenNoProvisionersAvailable(t *testing.T) {
1724
1723
p , err := coderdtest .GetProvisionerForTags (db , time .Now (), workspace .OrganizationID , provisionerDaemonTags )
1725
1724
require .NoError (t , err , "Error getting provisioner for workspace" )
1726
1725
1727
- var wg sync.WaitGroup
1728
- wg .Add (2 )
1726
+ // We're going to use an artificial next scheduled autostart time, as opposed to calculating it via sched.Next, since
1727
+ // we want to assert/require specific behavior here around the provisioner being stale, and therefore we need to be
1728
+ // able to give the provisioner(s) specific `LastSeenAt` times while dealing with the contraint that we cannot set
1729
+ // that value to some time in the past (relative to it's current value).
1730
+ next := p .LastSeenAt .Time .Add (5 * time .Minute )
1731
+ staleTime := next .Add (- (provisionerdserver .StaleInterval + time .Second ))
1732
+ coderdtest .UpdateProvisionerLastSeenAt (t , db , p .ID , staleTime )
1729
1733
1730
- next := sched .Next (workspace .LatestBuild .CreatedAt )
1731
- go func () {
1732
- defer wg .Done ()
1733
- // Ensure the provisioner is stale
1734
- staleTime := next .Add (- (provisionerdserver .StaleInterval * 2 ))
1735
- coderdtest .UpdateProvisionerLastSeenAt (t , db , p .ID , staleTime )
1736
- p , err = coderdtest .GetProvisionerForTags (db , time .Now (), workspace .OrganizationID , provisionerDaemonTags )
1737
- assert .NoError (t , err , "Error getting provisioner for workspace" )
1738
- assert .Eventually (t , func () bool { return p .LastSeenAt .Time .UnixNano () == staleTime .UnixNano () }, testutil .WaitMedium , testutil .IntervalFast )
1739
- }()
1740
-
1741
- go func () {
1742
- defer wg .Done ()
1743
- // Ensure the provisioner is gone or stale before triggering the autobuild
1744
- coderdtest .MustWaitForProvisionersUnavailable (t , db , workspace , provisionerDaemonTags , next )
1745
- // Trigger autobuild
1746
- tickCh <- next
1747
- }()
1734
+ // Require that the provisioners LastSeenAt has been updated to the expected time.
1735
+ p , err = coderdtest .GetProvisionerForTags (db , time .Now (), workspace .OrganizationID , provisionerDaemonTags )
1736
+ require .NoError (t , err , "Error getting provisioner for workspace" )
1737
+ // This assertion *may* no longer need to be `Eventually`.
1738
+ require .Eventually (t , func () bool { return p .LastSeenAt .Time .UnixNano () == staleTime .UnixNano () },
1739
+ testutil .WaitMedium , testutil .IntervalFast , "expected provisioner LastSeenAt to be:%+v, saw :%+v" , staleTime .UTC (), p .LastSeenAt .Time .UTC ())
1748
1740
1749
- wg .Wait ()
1741
+ // Ensure the provisioner is gone or stale, relative to the artificial next autostart time, before triggering the autobuild.
1742
+ coderdtest .MustWaitForProvisionersUnavailable (t , db , workspace , provisionerDaemonTags , next )
1750
1743
1744
+ // Trigger autobuild.
1745
+ tickCh <- next
1751
1746
stats := <- statsCh
1752
-
1753
- // This assertion should FAIL when provisioner is available (not stale), can confirm by commenting out the
1754
- // UpdateProvisionerLastSeenAt call above.
1755
1747
assert .Len (t , stats .Transitions , 0 , "should not create builds when no provisioners available" )
1756
1748
1757
1749
daemon2Closer := coderdtest .NewTaggedProvisionerDaemon (t , api , "name" , provisionerDaemonTags )
@@ -1762,12 +1754,18 @@ func TestExecutorAutostartSkipsWhenNoProvisionersAvailable(t *testing.T) {
1762
1754
// Ensure the provisioner is NOT stale, and see if we get a successful state transition.
1763
1755
p , err = coderdtest .GetProvisionerForTags (db , time .Now (), workspace .OrganizationID , provisionerDaemonTags )
1764
1756
require .NoError (t , err , "Error getting provisioner for workspace" )
1765
- notStaleTime := sched .Next (workspace .LatestBuild .CreatedAt ).Add ((- 1 * provisionerdserver .StaleInterval ) + 10 * time .Second )
1757
+
1758
+ next = sched .Next (workspace .LatestBuild .CreatedAt )
1759
+ notStaleTime := next .Add ((- 1 * provisionerdserver .StaleInterval ) + 10 * time .Second )
1766
1760
coderdtest .UpdateProvisionerLastSeenAt (t , db , p .ID , notStaleTime )
1761
+ // Require that the provisioner time has actually been updated to the expected value.
1762
+ p , err = coderdtest .GetProvisionerForTags (db , time .Now (), workspace .OrganizationID , provisionerDaemonTags )
1763
+ require .NoError (t , err , "Error getting provisioner for workspace" )
1764
+ require .True (t , next .UnixNano () > p .LastSeenAt .Time .UnixNano ())
1767
1765
1768
1766
// Trigger autobuild
1769
1767
go func () {
1770
- tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
1768
+ tickCh <- next
1771
1769
close (tickCh )
1772
1770
}()
1773
1771
stats = <- statsCh
0 commit comments