github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/statemgr/statemgr_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package statemgr 5 6 import ( 7 "context" 8 "encoding/json" 9 "flag" 10 "os" 11 "testing" 12 "time" 13 14 _ "github.com/terramate-io/tf/logging" 15 ) 16 17 func TestNewLockInfo(t *testing.T) { 18 info1 := NewLockInfo() 19 info2 := NewLockInfo() 20 21 if info1.ID == "" { 22 t.Fatal("LockInfo missing ID") 23 } 24 25 if info1.Version == "" { 26 t.Fatal("LockInfo missing version") 27 } 28 29 if info1.Created.IsZero() { 30 t.Fatal("LockInfo missing Created") 31 } 32 33 if info1.ID == info2.ID { 34 t.Fatal("multiple LockInfo with identical IDs") 35 } 36 37 // test the JSON output is valid 38 newInfo := &LockInfo{} 39 err := json.Unmarshal(info1.Marshal(), newInfo) 40 if err != nil { 41 t.Fatal(err) 42 } 43 } 44 45 func TestLockWithContext(t *testing.T) { 46 s := NewFullFake(nil, TestFullInitialState()) 47 48 id, err := s.Lock(NewLockInfo()) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 // use a cancelled context for an immediate timeout 54 ctx, cancel := context.WithCancel(context.Background()) 55 cancel() 56 57 info := NewLockInfo() 58 info.Info = "lock with context" 59 _, err = LockWithContext(ctx, s, info) 60 if err == nil { 61 t.Fatal("lock should have failed immediately") 62 } 63 64 // block until LockwithContext has made a first attempt 65 attempted := make(chan struct{}) 66 postLockHook = func() { 67 close(attempted) 68 postLockHook = nil 69 } 70 71 // unlock the state during LockWithContext 72 unlocked := make(chan struct{}) 73 var unlockErr error 74 go func() { 75 defer close(unlocked) 76 <-attempted 77 unlockErr = s.Unlock(id) 78 }() 79 80 ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second) 81 defer cancel() 82 83 id, err = LockWithContext(ctx, s, info) 84 if err != nil { 85 t.Fatal("lock should have completed within 2s:", err) 86 } 87 88 // ensure the goruotine completes 89 <-unlocked 90 if unlockErr != nil { 91 t.Fatal(unlockErr) 92 } 93 } 94 95 func TestMain(m *testing.M) { 96 flag.Parse() 97 os.Exit(m.Run()) 98 }