github.com/erda-project/erda-infra@v1.0.9/examples/example/helloworld/provider_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package example
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/erda-project/erda-infra/base/servicehub"
    22  )
    23  
    24  type testInterface interface {
    25  	testFunc(arg interface{}) interface{}
    26  }
    27  
    28  func (p *provider) testFunc(arg interface{}) interface{} {
    29  	return fmt.Sprintf("%s -> result", arg)
    30  }
    31  
    32  func Test_provider(t *testing.T) {
    33  	tests := []struct {
    34  		name     string
    35  		provider string
    36  		config   string
    37  		arg      interface{}
    38  		want     interface{}
    39  	}{
    40  		{
    41  			"case 1",
    42  			"helloworld",
    43  			`
    44  helloworld:
    45      message: "hello"
    46  `,
    47  
    48  			"test arg",
    49  			"test arg -> result",
    50  		},
    51  	}
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			hub := servicehub.New()
    55  			events := hub.Events()
    56  			go func() {
    57  				hub.RunWithOptions(&servicehub.RunOptions{Content: tt.config})
    58  			}()
    59  			<-events.Started()
    60  
    61  			p := hub.Provider(tt.provider).(*provider)
    62  			if got := p.testFunc(tt.arg); got != tt.want {
    63  				t.Errorf("provider.testFunc() = %v, want %v", got, tt.want)
    64  			}
    65  			if err := hub.Close(); err != nil {
    66  				t.Errorf("Hub.Close() = %v, want nil", err)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func Test_provider_service(t *testing.T) {
    73  	tests := []struct {
    74  		name    string
    75  		service string
    76  		config  string
    77  		arg     interface{}
    78  		want    interface{}
    79  	}{
    80  		{
    81  			"case 1",
    82  			"helloworld-service",
    83  			`
    84  helloworld:
    85      message: "hello"
    86  `,
    87  
    88  			"test arg",
    89  			"test arg -> result",
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			hub := servicehub.New()
    95  			events := hub.Events()
    96  			go func() {
    97  				hub.RunWithOptions(&servicehub.RunOptions{Content: tt.config})
    98  			}()
    99  			<-events.Started()
   100  			s := hub.Service(tt.service).(testInterface)
   101  			if got := s.testFunc(tt.arg); got != tt.want {
   102  				t.Errorf("(service %q).testFunc() = %v, want %v", tt.service, got, tt.want)
   103  			}
   104  			if err := hub.Close(); err != nil {
   105  				t.Errorf("Hub.Close() = %v, want nil", err)
   106  			}
   107  		})
   108  	}
   109  }