Skip to content

Commit

Permalink
test: shutdown priority
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 24, 2024
1 parent c031e75 commit 5165da3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 9 additions & 3 deletions shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ func init() {
}

var Shutdown = sync.OnceFunc(func() {
logger.Infof("shutting down")
for _, task := range shutdownTaskRegistry {
logger.Infof("begin shutdown")
for len(shutdownTaskRegistry) > 0 {
_task := heap.Pop(&shutdownTaskRegistry)
if _task == nil {
break
}

task := _task.(ShutdownTask)
logger.Infof("shutting down: %s", task.Label)
task.Hook()
}
Expand Down Expand Up @@ -79,7 +85,7 @@ func (st ShutdownTasks) Len() int { return len(st) }

// Less defines higher priority numbers will be processed first
func (st ShutdownTasks) Less(i, j int) bool {
return st[i].Priority > st[j].Priority // Higher priority numbers come first
return st[i].Priority < st[j].Priority // Higher priority numbers come first
}

func (st ShutdownTasks) Swap(i, j int) {
Expand Down
25 changes: 25 additions & 0 deletions shutdown/shutdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package shutdown

import "testing"

func TestShutdownPriority(t *testing.T) {
var closer int

add := func(label string, priority int) {
AddHookWithPriority(label, priority, func() {
if closer > priority {
t.Fatalf("something higher priority (%d) was closed earlier than (%d)", closer, priority)
} else {
closer = priority
}
})
}

add("database", PriorityCritical)
add("gRPC", PriorityIngress)
add("checkJOB", PriorityJobs)
add("echo", PriorityIngress)
add("topologyJob", PriorityJobs)

Shutdown()
}

0 comments on commit 5165da3

Please sign in to comment.