github.com/kumasuke120/mockuma@v1.1.9/internal/server/server_test.go (about)

     1  package server
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestNewMockServer(t *testing.T) {
    12  	//noinspection GoImportUsedAsName
    13  	assert := assert.New(t)
    14  
    15  	s := NewMockServer(1234)
    16  	assert.Equal(s.port, 1234)
    17  }
    18  
    19  func TestMockServer_Start(t *testing.T) {
    20  	//noinspection GoImportUsedAsName
    21  	assert := assert.New(t)
    22  
    23  	s := NewMockServer(3214)
    24  
    25  	assert.Panics(func() {
    26  		s.ListenAndServe(nil)
    27  	})
    28  
    29  	go func() {
    30  		time.Sleep(2 * time.Second)
    31  		assert.True(s.shutdown())
    32  	}()
    33  	s.ListenAndServe(mappings)
    34  }
    35  
    36  func TestMockServer_SetMappings(t *testing.T) {
    37  	//noinspection GoImportUsedAsName
    38  	assert := assert.New(t)
    39  
    40  	s := NewMockServer(3214)
    41  
    42  	assert.Panics(func() {
    43  		s.SetMappings(nil)
    44  	})
    45  
    46  	s.SetMappings(mappings)
    47  
    48  	var wg sync.WaitGroup
    49  	wg.Add(1)
    50  	go s.ListenAndServe(mappings)
    51  	go func() {
    52  		defer wg.Done()
    53  
    54  		time.Sleep(1 * time.Second)
    55  		s.SetMappings(mappings)
    56  		time.Sleep(2 * time.Second)
    57  		assert.True(s.shutdown())
    58  	}()
    59  	wg.Wait()
    60  }