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 future2021import (22 "context"23 "errors"24 "testing"25 "time"26)2728func TestFuture_SetBeforeGet(t *testing.T) {29 f := New()3031 f.Set(1, errors.New("1"))32 val, err := f.Get()33 if err.Error() != "1" {34 t.Error("Wrong error:", err)35 }3637 if val, _ := val.(int); val != 1 {38 t.Fatal("wrong val received from Get")39 }40}4142func TestFuture_Wait(t *testing.T) {43 f := New()4445 go func() {46 time.Sleep(500 * time.Millisecond)47 f.Set(1, errors.New("1"))48 }()4950 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 }5758 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}6667func 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}