github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/model/suppressor_test.go (about) 1 // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file). 2 // All rights reserved. Use of this source code is governed by an MIT-style 3 // license that can be found in the LICENSE file. 4 5 package model 6 7 import ( 8 "testing" 9 "time" 10 ) 11 12 func TestSuppressor(t *testing.T) { 13 s := suppressor{threshold: 10000} 14 t0 := time.Now() 15 16 t1 := t0 17 sup, prev := s.suppress("foo", 10000, t1) 18 if sup { 19 t.Fatal("Never suppress first change") 20 } 21 if prev { 22 t.Fatal("Incorrect prev status") 23 } 24 25 // bw is 10000 / 10 = 1000 26 t1 = t0.Add(10 * time.Second) 27 if bw := s.changes["foo"].bandwidth(t1); bw != 1000 { 28 t.Errorf("Incorrect bw %d", bw) 29 } 30 sup, prev = s.suppress("foo", 10000, t1) 31 if sup { 32 t.Fatal("Should still be fine") 33 } 34 if prev { 35 t.Fatal("Incorrect prev status") 36 } 37 38 // bw is (10000 + 10000) / 11 = 1818 39 t1 = t0.Add(11 * time.Second) 40 if bw := s.changes["foo"].bandwidth(t1); bw != 1818 { 41 t.Errorf("Incorrect bw %d", bw) 42 } 43 sup, prev = s.suppress("foo", 100500, t1) 44 if sup { 45 t.Fatal("Should still be fine") 46 } 47 if prev { 48 t.Fatal("Incorrect prev status") 49 } 50 51 // bw is (10000 + 10000 + 100500) / 12 = 10041 52 t1 = t0.Add(12 * time.Second) 53 if bw := s.changes["foo"].bandwidth(t1); bw != 10041 { 54 t.Errorf("Incorrect bw %d", bw) 55 } 56 sup, prev = s.suppress("foo", 10000000, t1) // value will be ignored 57 if !sup { 58 t.Fatal("Should be over threshold") 59 } 60 if prev { 61 t.Fatal("Incorrect prev status") 62 } 63 64 // bw is (10000 + 10000 + 100500) / 15 = 8033 65 t1 = t0.Add(15 * time.Second) 66 if bw := s.changes["foo"].bandwidth(t1); bw != 8033 { 67 t.Errorf("Incorrect bw %d", bw) 68 } 69 sup, prev = s.suppress("foo", 10000000, t1) 70 if sup { 71 t.Fatal("Should be Ok") 72 } 73 if !prev { 74 t.Fatal("Incorrect prev status") 75 } 76 } 77 78 func TestHistory(t *testing.T) { 79 h := changeHistory{} 80 81 t0 := time.Now() 82 h.append(40, t0) 83 84 if l := len(h.changes); l != 1 { 85 t.Errorf("Incorrect history length %d", l) 86 } 87 if s := h.changes[0].size; s != 40 { 88 t.Errorf("Incorrect first record size %d", s) 89 } 90 91 for i := 1; i < MaxChangeHistory; i++ { 92 h.append(int64(40+i), t0.Add(time.Duration(i)*time.Second)) 93 } 94 95 if l := len(h.changes); l != MaxChangeHistory { 96 t.Errorf("Incorrect history length %d", l) 97 } 98 if s := h.changes[0].size; s != 40 { 99 t.Errorf("Incorrect first record size %d", s) 100 } 101 if s := h.changes[MaxChangeHistory-1].size; s != 40+MaxChangeHistory-1 { 102 t.Errorf("Incorrect last record size %d", s) 103 } 104 105 h.append(999, t0.Add(time.Duration(999)*time.Second)) 106 107 if l := len(h.changes); l != MaxChangeHistory { 108 t.Errorf("Incorrect history length %d", l) 109 } 110 if s := h.changes[0].size; s != 41 { 111 t.Errorf("Incorrect first record size %d", s) 112 } 113 if s := h.changes[MaxChangeHistory-1].size; s != 999 { 114 t.Errorf("Incorrect last record size %d", s) 115 } 116 117 }