github.com/Tri-stone/burrow@v0.25.0/vent/service/server_test.go (about)

     1  // +build integration
     2  
     3  package service_test
     4  
     5  import (
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"os"
    10  	"sync"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/hyperledger/burrow/execution/evm/abi"
    15  	"github.com/hyperledger/burrow/integration"
    16  	"github.com/hyperledger/burrow/integration/rpctest"
    17  	"github.com/hyperledger/burrow/vent/config"
    18  	"github.com/hyperledger/burrow/vent/logger"
    19  	"github.com/hyperledger/burrow/vent/service"
    20  	"github.com/hyperledger/burrow/vent/sqlsol"
    21  	"github.com/hyperledger/burrow/vent/test"
    22  	"github.com/hyperledger/burrow/vent/types"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestServer(t *testing.T) {
    27  	kern, shutdown := integration.RunNode(t, rpctest.GenesisDoc, rpctest.PrivateAccounts)
    28  	defer shutdown()
    29  	t.Parallel()
    30  
    31  	t.Run("Group", func(t *testing.T) {
    32  		t.Run("Run", func(t *testing.T) {
    33  			// run consumer to listen to events
    34  			cfg := config.DefaultVentConfig()
    35  
    36  			// create test db
    37  			_, closeDB := test.NewTestDB(t, cfg)
    38  			defer closeDB()
    39  
    40  			cfg.SpecFileOrDirs = []string{os.Getenv("GOPATH") + "/src/github.com/hyperledger/burrow/vent/test/sqlsol_example.json"}
    41  			cfg.AbiFileOrDirs = []string{os.Getenv("GOPATH") + "/src/github.com/hyperledger/burrow/vent/test/EventsTest.abi"}
    42  			cfg.GRPCAddr = kern.GRPCListenAddress().String()
    43  
    44  			log := logger.NewLogger(cfg.LogLevel)
    45  			consumer := service.NewConsumer(cfg, log, make(chan types.EventData))
    46  
    47  			projection, err := sqlsol.SpecLoader(cfg.SpecFileOrDirs, false)
    48  			abiSpec, err := abi.LoadPath(cfg.AbiFileOrDirs...)
    49  
    50  			var wg sync.WaitGroup
    51  
    52  			wg.Add(1)
    53  			go func() {
    54  				err := consumer.Run(projection, abiSpec, true)
    55  				require.NoError(t, err)
    56  
    57  				wg.Done()
    58  			}()
    59  
    60  			time.Sleep(2 * time.Second)
    61  
    62  			// setup test server
    63  			server := service.NewServer(cfg, log, consumer)
    64  
    65  			httpServer := httptest.NewServer(server)
    66  			defer httpServer.Close()
    67  
    68  			// call health endpoint should return OK
    69  			healthURL := fmt.Sprintf("%s/health", httpServer.URL)
    70  
    71  			resp, err := http.Get(healthURL)
    72  			require.NoError(t, err)
    73  			require.Equal(t, http.StatusOK, resp.StatusCode)
    74  
    75  			// shutdown consumer and wait for its end
    76  			consumer.Shutdown()
    77  			wg.Wait()
    78  
    79  			// call health endpoint again should return error
    80  			resp, err = http.Get(healthURL)
    81  			require.NoError(t, err)
    82  			require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
    83  		})
    84  	})
    85  }