github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/service_info_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 ) 11 12 func TestWaitForServiceInfoOK(t *testing.T) { 13 fn := func() (*ServiceInfo, error) { 14 return &ServiceInfo{Label: "ok", Pid: 1}, nil 15 } 16 info, err := waitForServiceInfo(time.Second, time.Millisecond, fn) 17 if err != nil { 18 t.Fatal(err) 19 } 20 if info == nil || info.Label != "ok" { 21 t.Fatalf("Invalid info") 22 } 23 } 24 25 func TestWaitForServiceInfoDelayed(t *testing.T) { 26 i := 0 27 fn := func() (*ServiceInfo, error) { 28 i++ 29 if i == 5 { 30 return &ServiceInfo{Label: "ok_delayed", Pid: 1}, nil 31 } 32 return nil, nil 33 } 34 info, err := waitForServiceInfo(time.Second, time.Millisecond, fn) 35 if err != nil { 36 t.Fatal(err) 37 } 38 if info == nil || info.Label != "ok_delayed" { 39 t.Fatalf("Invalid status") 40 } 41 } 42 43 func TestWaitForServiceInfoErrored(t *testing.T) { 44 fn := func() (*ServiceInfo, error) { 45 return nil, fmt.Errorf("info error") 46 } 47 _, err := waitForServiceInfo(time.Second, time.Millisecond, fn) 48 if err == nil { 49 t.Fatal("Expected error") 50 } 51 if err.Error() != "info error" { 52 t.Fatal("Expected error returned from fn above") 53 } 54 } 55 56 func TestWaitForServiceInfoTimeout(t *testing.T) { 57 fn := func() (*ServiceInfo, error) { 58 return nil, nil 59 } 60 status, err := waitForServiceInfo(5*time.Millisecond, time.Millisecond, fn) 61 if err != nil { 62 t.Fatal(err) 63 } 64 if status != nil { 65 t.Fatalf("Info should be nil (timed out): %#v", status) 66 } 67 }