github.com/jlowellwofford/u-root@v1.0.0/pkg/sos/service_test.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sos
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"sync"
    11  	"testing"
    12  )
    13  
    14  type RegistryEntryStub struct {
    15  	service string
    16  	port    uint
    17  }
    18  
    19  var (
    20  	knownServ1 = RegistryEntryStub{"stub1", 1}
    21  	knownServ2 = RegistryEntryStub{"stub2", 2}
    22  	knownServ3 = RegistryEntryStub{"stub3", 3}
    23  	newServ1   = RegistryEntryStub{"stub4", 4}
    24  	newServ2   = RegistryEntryStub{"stub5", 5}
    25  	newServ3   = RegistryEntryStub{"stub6", 6}
    26  )
    27  
    28  func setUpKnownServices() *SosService {
    29  	service := NewSosService()
    30  	service.registry[knownServ1.service] = knownServ1.port
    31  	service.registry[knownServ2.service] = knownServ2.port
    32  	service.registry[knownServ3.service] = knownServ3.port
    33  	return service
    34  }
    35  
    36  func TestReadNonExist(t *testing.T) {
    37  	s := NewSosService()
    38  	if _, err := s.Read(knownServ1.service); !reflect.DeepEqual(err, fmt.Errorf("%v is not in the registry", knownServ1.service)) {
    39  		t.Errorf("read(%v)\ngot:(%v)\nwant:(%v)", knownServ1.service, err, fmt.Errorf("%v is not in the registry", knownServ1.service))
    40  	}
    41  }
    42  
    43  func TestRead(t *testing.T) {
    44  	s := setUpKnownServices()
    45  	if port, err := s.Read(knownServ1.service); err != nil || port != knownServ1.port {
    46  		t.Errorf("read(%v)\ngot:(%v, %v)\nwant:(%v, %v)", knownServ1.service, port, err, knownServ1.port, nil)
    47  	}
    48  }
    49  
    50  func TestRegisterAlreadyExists(t *testing.T) {
    51  	s := setUpKnownServices()
    52  	err := s.Register(knownServ1.service, knownServ1.port)
    53  	if !reflect.DeepEqual(err, fmt.Errorf("%v already exists", knownServ1.service)) {
    54  		t.Errorf("Already Exists Register\ngot:(%v)\nwant:(%v)", err, fmt.Errorf("%v already exists", knownServ1.service))
    55  	}
    56  }
    57  
    58  func TestRegisterSuccess(t *testing.T) {
    59  	s := NewSosService()
    60  	s.Register(knownServ1.service, knownServ1.port)
    61  	if port, err := s.Read(knownServ1.service); err != nil || port != knownServ1.port {
    62  		t.Errorf("register(%v)\ngot:(%v, %v)\nwant:(%v, %v)", knownServ1, port, err, knownServ1.port, nil)
    63  	}
    64  }
    65  
    66  func TestUnregisterNonExist(t *testing.T) {
    67  	s := NewSosService()
    68  	s.Unregister(knownServ1.service)
    69  	// should not panic
    70  }
    71  
    72  func TestUnregister(t *testing.T) {
    73  	s := setUpKnownServices()
    74  	s.Unregister(knownServ1.service)
    75  	if _, err := s.Read(knownServ1.service); !reflect.DeepEqual(err, fmt.Errorf("%v is not in the registry", knownServ1.service)) {
    76  		t.Errorf("unregister(%v)\ngot:(%v)\nwant:(%v)", knownServ1.service, err, fmt.Errorf("%v is not in the registry", knownServ1.service))
    77  	}
    78  }
    79  
    80  func TestSnapshot(t *testing.T) {
    81  	s := setUpKnownServices().SnapshotRegistry()
    82  	if port, exists := s[knownServ1.service]; !exists || port != knownServ1.port {
    83  		t.Errorf("%v\ngot:(%v, %v)\nwant:(%v, %v)", knownServ1, port, exists, knownServ1.port, true)
    84  	}
    85  	if port, exists := s[knownServ2.service]; !exists || port != knownServ2.port {
    86  		t.Errorf("%v\ngot:(%v, %v)\nwant:(%v, %v)", knownServ2, port, exists, knownServ2.port, true)
    87  	}
    88  	if port, exists := s[knownServ3.service]; !exists || port != knownServ3.port {
    89  		t.Errorf("%v\ngot:(%v, %v)\nwant:(%v, %v)", knownServ3, port, exists, knownServ1.port, true)
    90  	}
    91  }
    92  
    93  func TestRaceCondtion(t *testing.T) {
    94  	//Set Up
    95  	s := NewSosService()
    96  	numReadGoRoutines := 10
    97  	numRegisterGoRoutines := 20
    98  	numUnregisterGoRoutines := 20
    99  	numSnapshotGoRoutines := 10
   100  
   101  	var wg sync.WaitGroup
   102  	for i := 0; i < numRegisterGoRoutines; i++ {
   103  		wg.Add(1)
   104  		go func(idx uint) {
   105  			defer wg.Done()
   106  			s.Register(fmt.Sprintf("stub%v", idx), idx)
   107  		}(uint(i))
   108  	}
   109  
   110  	for i := 0; i < numReadGoRoutines; i++ {
   111  		wg.Add(1)
   112  		go func(idx uint) {
   113  			defer wg.Done()
   114  			s.Read(fmt.Sprintf("stub%v", idx))
   115  		}(uint(i))
   116  	}
   117  
   118  	for i := 0; i < numUnregisterGoRoutines; i++ {
   119  		wg.Add(1)
   120  		go func(idx uint) {
   121  			defer wg.Done()
   122  			s.Unregister(fmt.Sprintf("stub%v", idx))
   123  		}(uint(i))
   124  	}
   125  
   126  	for i := 0; i < numSnapshotGoRoutines; i++ {
   127  		wg.Add(1)
   128  		go func() {
   129  			defer wg.Done()
   130  			s.SnapshotRegistry()
   131  		}()
   132  	}
   133  
   134  	wg.Wait()
   135  }