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 future
20
21import (
22	"context"
23	"errors"
24	"testing"
25	"time"
26)
27
28func TestFuture_SetBeforeGet(t *testing.T) {
29	f := New()
30
31	f.Set(1, errors.New("1"))
32	val, err := f.Get()
33	if err.Error() != "1" {
34		t.Error("Wrong error:", err)
35	}
36
37	if val, _ := val.(int); val != 1 {
38		t.Fatal("wrong val received from Get")
39	}
40}
41
42func TestFuture_Wait(t *testing.T) {
43	f := New()
44
45	go func() {
46		time.Sleep(500 * time.Millisecond)
47		f.Set(1, errors.New("1"))
48	}()
49
50	val, err := f.Get()
51	if val, _ := val.(int); val != 1 {
52		t.Fatal("wrong val received from Get")
53	}
54	if err.Error() != "1" {
55		t.Error("Wrong error:", err)
56	}
57
58	val, err = f.Get()
59	if val, _ := val.(int); val != 1 {
60		t.Fatal("wrong val received from Get on second try")
61	}
62	if err.Error() != "1" {
63		t.Error("Wrong error:", err)
64	}
65}
66
67func TestFuture_WaitCtx(t *testing.T) {
68	f := New()
69	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
70	defer cancel()
71	_, err := f.GetContext(ctx)
72	if !errors.Is(err, context.DeadlineExceeded) {
73		t.Fatal("context is not cancelled")
74	}
75}