github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/progress/progress_test.go (about) 1 // Copyright 2021 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package progress 6 7 import ( 8 "testing" 9 "time" 10 ) 11 12 func TestProgressBegin(t *testing.T) { 13 tests := []struct { 14 name string 15 mode string 16 sendQuit bool 17 wait time.Duration 18 }{ 19 { 20 name: "Progress Begin", 21 mode: "none", 22 sendQuit: false, 23 wait: 0, 24 }, 25 { 26 name: "Progress mode progress", 27 mode: "progress", 28 sendQuit: true, 29 wait: 2, 30 }, 31 } 32 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 someVariable := int64(1) 36 p := Begin(tt.mode, &someVariable) 37 38 if p == nil { 39 t.Errorf(`Begin(%q, %v) = %v, want not nil`, tt.mode, &someVariable, p) 40 } 41 42 time.Sleep(tt.wait * time.Second) 43 44 if tt.sendQuit { 45 p.quit <- struct{}{} 46 } 47 }) 48 } 49 } 50 51 func TestProgressEnd(t *testing.T) { 52 tests := []struct { 53 name string 54 mode string 55 wait time.Duration 56 }{ 57 { 58 name: "Mode none", 59 mode: "none", 60 wait: 1, 61 }, 62 { 63 name: "Mode progress", 64 mode: "progress", 65 wait: 1, 66 }, 67 { 68 name: "Mode xfer", 69 mode: "xfer", 70 wait: 1, 71 }, 72 } 73 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 someVariable := int64(1) 77 p := Begin(tt.mode, &someVariable) 78 79 if p == nil { 80 t.Fatal("Progress Structure is nil") 81 } 82 83 time.Sleep(tt.wait * time.Second) 84 85 p.End() 86 87 // Looks like this check is sometimes faster than the channel 88 time.Sleep(50 * time.Millisecond) 89 90 p.endTimeMutex.Lock() 91 if p.end.IsZero() { 92 t.Errorf(`Begin(%q, %v).end.IsZero() = %t, want false`, tt.mode, &someVariable, p.end.IsZero()) 93 } 94 p.endTimeMutex.Unlock() 95 }) 96 } 97 }