maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package queue
 20
 21import (
 22	"testing"
 23	"time"
 24)
 25
 26func TestTimeWheelAdd(t *testing.T) {
 27	t.Parallel()
 28
 29	called := make(chan TimeSlot)
 30
 31	w := NewTimeWheel(func(slot TimeSlot) {
 32		called <- slot
 33	})
 34	defer w.Close()
 35
 36	w.Add(time.Now().Add(1*time.Second), 1)
 37
 38	slot := <-called
 39	if val, _ := slot.Value.(int); val != 1 {
 40		t.Errorf("Wrong slot value: %v", slot.Value)
 41	}
 42}
 43
 44func TestTimeWheelAdd_Ordering(t *testing.T) {
 45	t.Parallel()
 46
 47	called := make(chan TimeSlot)
 48
 49	w := NewTimeWheel(func(slot TimeSlot) {
 50		called <- slot
 51	})
 52	defer w.Close()
 53
 54	w.Add(time.Now().Add(1*time.Second), 1)
 55	w.Add(time.Now().Add(1250*time.Millisecond), 2)
 56
 57	slot := <-called
 58	if val, _ := slot.Value.(int); val != 1 {
 59		t.Errorf("Wrong first slot value: %v", slot.Value)
 60	}
 61	slot = <-called
 62	if val, _ := slot.Value.(int); val != 2 {
 63		t.Errorf("Wrong second slot value: %v", slot.Value)
 64	}
 65}
 66
 67func TestTimeWheelAdd_Restart(t *testing.T) {
 68	t.Parallel()
 69
 70	called := make(chan TimeSlot)
 71
 72	w := NewTimeWheel(func(slot TimeSlot) {
 73		called <- slot
 74	})
 75	defer w.Close()
 76
 77	w.Add(time.Now().Add(1*time.Second), 1)
 78	w.Add(time.Now().Add(500*time.Millisecond), 2)
 79
 80	slot := <-called
 81	if val, _ := slot.Value.(int); val != 2 {
 82		t.Errorf("Wrong first slot value: %v", slot.Value)
 83	}
 84	slot = <-called
 85	if val, _ := slot.Value.(int); val != 1 {
 86		t.Errorf("Wrong second slot value: %v", slot.Value)
 87	}
 88}
 89
 90func TestTimeWheelAdd_MissingGotoBug(t *testing.T) {
 91	t.Parallel()
 92
 93	called := make(chan TimeSlot)
 94
 95	w := NewTimeWheel(func(slot TimeSlot) {
 96		called <- slot
 97	})
 98	defer w.Close()
 99
100	w.Add(time.Now().Add(90000*time.Hour), 1)      // practically newer
101	w.Add(time.Now().Add(500*time.Millisecond), 2) // should correctly restart
102
103	slot := <-called
104	if val, _ := slot.Value.(int); val != 2 {
105		t.Errorf("Wrong first slot value: %v", slot.Value)
106	}
107}
108
109func TestTimeWheelAdd_EmptyUpdWait(t *testing.T) {
110	t.Parallel()
111
112	called := make(chan TimeSlot)
113
114	w := NewTimeWheel(func(slot TimeSlot) {
115		called <- slot
116	})
117	defer w.Close()
118
119	time.Sleep(500 * time.Millisecond)
120
121	w.Add(time.Now().Add(1*time.Second), 1)
122
123	slot := <-called
124	if val, _ := slot.Value.(int); val != 1 {
125		t.Errorf("Wrong slot value: %v", slot.Value)
126	}
127}