1// SPDX-License-Identifier: ISC
2// Copyright (c) 2014-2020 Bitmark Inc.
3// Use of this source code is governed by an ISC
4// license that can be found in the LICENSE file.
5
6package domain_test
7
8import (
9	"fmt"
10	"sync"
11	"testing"
12
13	"github.com/golang/mock/gomock"
14	"github.com/stretchr/testify/assert"
15
16	"github.com/bitmark-inc/bitmarkd/announce/domain"
17	"github.com/bitmark-inc/bitmarkd/announce/fixtures"
18	"github.com/bitmark-inc/bitmarkd/announce/mocks"
19	"github.com/bitmark-inc/logger"
20)
21
22func TestNewDomain(t *testing.T) {
23	fixtures.SetupTestLogger()
24	defer fixtures.TeardownTestLogger()
25
26	ctl := gomock.NewController(t)
27	defer ctl.Finish()
28
29	r := mocks.NewMockReceptor(ctl)
30	f := func(s string) ([]string, error) { return []string{}, nil }
31
32	_, err := domain.New(logger.New(fixtures.LogCategory), "domain.not.exist", r, f)
33	assert.Nil(t, err, "wrong NewDomain")
34}
35
36func TestNewDomainWhenLookupError(t *testing.T) {
37	fixtures.SetupTestLogger()
38	defer fixtures.TeardownTestLogger()
39
40	ctl := gomock.NewController(t)
41	defer ctl.Finish()
42
43	r := mocks.NewMockReceptor(ctl)
44	f := func(s string) ([]string, error) {
45		return []string{}, fmt.Errorf("error")
46	}
47
48	_, err := domain.New(logger.New(fixtures.LogCategory), "domain.not.exist", r, f)
49	assert.Equal(t, fmt.Errorf("error"), err, "wrong NewDomain")
50}
51
52func TestRunWhenShutdown(t *testing.T) {
53	fixtures.SetupTestLogger()
54	defer fixtures.TeardownTestLogger()
55
56	ctl := gomock.NewController(t)
57	defer ctl.Finish()
58
59	r := mocks.NewMockReceptor(ctl)
60	f := func(s string) ([]string, error) { return []string{}, nil }
61
62	b, _ := domain.New(logger.New(fixtures.LogCategory), "domain.not.exist", r, f)
63
64	shutdown := make(chan struct{})
65	wg := new(sync.WaitGroup)
66	wg.Add(1)
67
68	go func(wg *sync.WaitGroup) {
69		b.Run(nil, shutdown)
70		wg.Done()
71	}(wg)
72
73	shutdown <- struct{}{}
74	wg.Wait()
75}
76