github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nbre/nbre_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package nbre
    20  
    21  import (
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/nebulasio/go-nebulas/core"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestNbre_Execute(t *testing.T) {
    31  	tests := []struct {
    32  		name    string
    33  		command string
    34  		params  []interface{}
    35  		result  interface{}
    36  		err     error
    37  	}{
    38  		{
    39  			name:    "command not found",
    40  			command: "commandNotFound",
    41  			params:  nil,
    42  			result:  nil,
    43  			err:     ErrCommandNotFound,
    44  		},
    45  		{
    46  			name:    "command version",
    47  			command: CommandVersion,
    48  			params:  []interface{}{uint64(1)},
    49  			result:  nil,
    50  			err:     ErrNebCallbackTimeout,
    51  		},
    52  	}
    53  
    54  	wg := new(sync.WaitGroup)
    55  	wg.Add(2)
    56  
    57  	neb := core.NewMockNeb(nil, nil, nil)
    58  
    59  	nbre := NewNbre(neb)
    60  	nbre.Start()
    61  	//assert.NoError(t, err, "nbre start failed")
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			defer wg.Done()
    66  
    67  			result, err := nbre.Execute(tt.command, tt.params...)
    68  			assert.Equal(t, tt.err, err)
    69  			assert.Equal(t, tt.result, result)
    70  		})
    71  	}
    72  
    73  	wg.Wait()
    74  
    75  	assert.Zero(t, len(nbreHandlers), "handlers not delete")
    76  
    77  	nbre.Stop()
    78  }