eintopf.info@v0.13.16/service/killswitch/store_test.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package killswitch_test 17 18 import ( 19 "context" 20 "os" 21 "testing" 22 "time" 23 24 "eintopf.info/service/killswitch" 25 "eintopf.info/service/killswitch/testutil" 26 ) 27 28 func TestMemoryStore(t *testing.T) { 29 store := killswitch.NewMemoryStore() 30 testutil.TestStore(t, store) 31 } 32 33 func TestFileStore(t *testing.T) { 34 store := killswitch.NewFileStore("test_killswitch", 5*time.Second) 35 defer store.Stop() 36 defer os.Remove("test_killswitch") 37 testutil.TestStore(t, store) 38 } 39 40 func TestFileStoreExternal(t *testing.T) { 41 store := killswitch.NewFileStore("test_killswitch_external", 10*time.Millisecond) 42 defer store.Stop() 43 defer os.Remove("test_killswitch_external") 44 45 err := store.SetState(context.Background(), false) 46 if err != nil { 47 t.Errorf("store.SetState(ctx, false): %s", err) 48 } 49 if _, err := os.Stat("test_killswitch_external"); err != nil { 50 t.Errorf("file should exist: %s", err) 51 } 52 err = os.Remove("test_killswitch_external") 53 if err != nil { 54 t.Errorf("os.Remove('test_killswitch_external'): %s", err) 55 } 56 time.Sleep(100 * time.Millisecond) 57 on, err := store.GetState(context.Background()) 58 if err != nil { 59 t.Errorf("store.GetState(ctx): %s", err) 60 } 61 if on != true { 62 t.Error("state: on != true, after delting file") 63 } 64 65 err = store.SetState(context.Background(), true) 66 if err != nil { 67 t.Errorf("store.SetState(ctx, true): %s", err) 68 } 69 if _, err := os.Stat("test_killswitch_external"); err == nil { 70 t.Errorf("file should not exist: %s", err) 71 } 72 err = os.WriteFile("test_killswitch_external", []byte{}, 0644) 73 if err != nil { 74 t.Errorf("os.WriteFile('test_killswitch_external', []byte{}, 0644): %s", err) 75 } 76 time.Sleep(100 * time.Millisecond) 77 on, err = store.GetState(context.Background()) 78 if err != nil { 79 t.Errorf("store.GetState(ctx): %s", err) 80 } 81 if on != false { 82 t.Error("state: on != false, after creating file") 83 } 84 }