github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchtransport/opensearchtransport_integration_multinode_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  // +build integration,multinode
    28  
    29  package opensearchtransport_test
    30  
    31  import (
    32  	"encoding/json"
    33  	"fmt"
    34  	"net/http"
    35  	"net/url"
    36  	"testing"
    37  
    38  	"github.com/opensearch-project/opensearch-go/v2/opensearchtransport"
    39  )
    40  
    41  var (
    42  	_ = fmt.Print
    43  )
    44  
    45  func TestTransportSelector(t *testing.T) {
    46  	NodeName := func(t *testing.T, transport opensearchtransport.Interface) string {
    47  		req, err := http.NewRequest("GET", "/", nil)
    48  		if err != nil {
    49  			t.Fatalf("Unexpected error: %s", err)
    50  		}
    51  
    52  		res, err := transport.Perform(req)
    53  		if err != nil {
    54  			t.Fatalf("Unexpected error: %s", err)
    55  		}
    56  
    57  		fmt.Printf("> GET %s\n", req.URL)
    58  
    59  		r := struct {
    60  			Name string
    61  		}{}
    62  
    63  		if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
    64  			t.Fatalf("Error parsing the response body: %s", err)
    65  		}
    66  
    67  		return r.Name
    68  	}
    69  
    70  	t.Run("RoundRobin", func(t *testing.T) {
    71  		var (
    72  			node string
    73  		)
    74  		transport, _ := opensearchtransport.New(opensearchtransport.Config{URLs: []*url.URL{
    75  			{Scheme: "http", Host: "localhost:9200"},
    76  			{Scheme: "http", Host: "localhost:9201"},
    77  		}})
    78  
    79  		node = NodeName(t, transport)
    80  		if node != "es1" {
    81  			t.Errorf("Unexpected node, want=e1, got=%s", node)
    82  		}
    83  
    84  		node = NodeName(t, transport)
    85  		if node != "es2" {
    86  			t.Errorf("Unexpected node, want=e1, got=%s", node)
    87  		}
    88  
    89  		node = NodeName(t, transport)
    90  		if node != "es1" {
    91  			t.Errorf("Unexpected node, want=e1, got=%s", node)
    92  		}
    93  	})
    94  }