1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package queue2021import (22 "testing"23 "time"24)2526func TestTimeWheelAdd(t *testing.T) {27 t.Parallel()2829 called := make(chan TimeSlot)3031 w := NewTimeWheel(func(slot TimeSlot) {32 called <- slot33 })34 defer w.Close()3536 w.Add(time.Now().Add(1*time.Second), 1)3738 slot := <-called39 if val, _ := slot.Value.(int); val != 1 {40 t.Errorf("Wrong slot value: %v", slot.Value)41 }42}4344func TestTimeWheelAdd_Ordering(t *testing.T) {45 t.Parallel()4647 called := make(chan TimeSlot)4849 w := NewTimeWheel(func(slot TimeSlot) {50 called <- slot51 })52 defer w.Close()5354 w.Add(time.Now().Add(1*time.Second), 1)55 w.Add(time.Now().Add(1250*time.Millisecond), 2)5657 slot := <-called58 if val, _ := slot.Value.(int); val != 1 {59 t.Errorf("Wrong first slot value: %v", slot.Value)60 }61 slot = <-called62 if val, _ := slot.Value.(int); val != 2 {63 t.Errorf("Wrong second slot value: %v", slot.Value)64 }65}6667func TestTimeWheelAdd_Restart(t *testing.T) {68 t.Parallel()6970 called := make(chan TimeSlot)7172 w := NewTimeWheel(func(slot TimeSlot) {73 called <- slot74 })75 defer w.Close()7677 w.Add(time.Now().Add(1*time.Second), 1)78 w.Add(time.Now().Add(500*time.Millisecond), 2)7980 slot := <-called81 if val, _ := slot.Value.(int); val != 2 {82 t.Errorf("Wrong first slot value: %v", slot.Value)83 }84 slot = <-called85 if val, _ := slot.Value.(int); val != 1 {86 t.Errorf("Wrong second slot value: %v", slot.Value)87 }88}8990func TestTimeWheelAdd_MissingGotoBug(t *testing.T) {91 t.Parallel()9293 called := make(chan TimeSlot)9495 w := NewTimeWheel(func(slot TimeSlot) {96 called <- slot97 })98 defer w.Close()99100 w.Add(time.Now().Add(90000*time.Hour), 1) // practically newer101 w.Add(time.Now().Add(500*time.Millisecond), 2) // should correctly restart102103 slot := <-called104 if val, _ := slot.Value.(int); val != 2 {105 t.Errorf("Wrong first slot value: %v", slot.Value)106 }107}108109func TestTimeWheelAdd_EmptyUpdWait(t *testing.T) {110 t.Parallel()111112 called := make(chan TimeSlot)113114 w := NewTimeWheel(func(slot TimeSlot) {115 called <- slot116 })117 defer w.Close()118119 time.Sleep(500 * time.Millisecond)120121 w.Add(time.Now().Add(1*time.Second), 1)122123 slot := <-called124 if val, _ := slot.Value.(int); val != 1 {125 t.Errorf("Wrong slot value: %v", slot.Value)126 }127}