github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/status/status_test.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 package status 6 7 import ( 8 "math/rand" 9 "testing" 10 ) 11 12 func TestStatus(t *testing.T) { 13 var status Status 14 if got, want := len(status.Groups()), 0; got != want { 15 t.Errorf("got %v, want %v", got, want) 16 } 17 c := status.Wait(-1) 18 var v int 19 select { 20 case v = <-c: 21 default: 22 t.Fatal("expected update") 23 } 24 if got, want := v, 0; got != want { 25 t.Errorf("got %v, want %v", got, want) 26 } 27 c = status.Wait(v) 28 select { 29 case <-c: 30 t.Fatal("unexpected update") 31 default: 32 } 33 g := status.Group("test") 34 select { 35 case v = <-c: 36 default: 37 t.Fatal("expected update") 38 } 39 if got, want := v, 1; got != want { 40 t.Errorf("got %v, want %v", got, want) 41 } 42 c = status.Wait(v) 43 task := g.Start("hello world") 44 select { 45 case v = <-c: 46 default: 47 t.Fatal("expected update") 48 } 49 c = status.Wait(v) 50 task.Print("an update") 51 select { 52 case v = <-c: 53 default: 54 t.Fatal("expected update") 55 } 56 groups := status.Groups() 57 if got, want := len(groups), 1; got != want { 58 t.Fatalf("got %v, want %v", got, want) 59 } 60 if got, want := groups[0], g; got != want { 61 t.Errorf("got %v, want %v", got, want) 62 } 63 g = groups[0] 64 if got, want := g.Value().Title, "test"; got != want { 65 t.Errorf("got %v, want %v", got, want) 66 } 67 tasks := g.Tasks() 68 if got, want := len(tasks), 1; got != want { 69 t.Fatalf("got %v, want %v", got, want) 70 } 71 task = tasks[0] 72 if got, want := task.Value().Title, "hello world"; got != want { 73 t.Errorf("got %v, want %v", got, want) 74 } 75 if got, want := task.Value().Status, "an update"; got != want { 76 t.Errorf("got %v, want %v", got, want) 77 } 78 } 79 80 func TestStatusManyWaiters(t *testing.T) { 81 var ( 82 status Status 83 versions = rand.Perm(100) 84 waiters = make([]<-chan int, len(versions)) 85 ) 86 for _, v := range versions { 87 waiters[v] = status.Wait(v) 88 } 89 for v := <-status.Wait(-1); v < len(versions); v++ { 90 for w := v; w < len(versions); w++ { 91 select { 92 case <-waiters[w]: 93 t.Errorf("unexpected ready waiter %v", w) 94 default: 95 } 96 } 97 status.notify() 98 select { 99 case <-waiters[v]: 100 default: 101 t.Errorf("expected waiter %v to be ready (version %d)", v, status.version) 102 } 103 } 104 }