vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/vrlog_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vreplication
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"strconv"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestVrLog(t *testing.T) {
    29  	r, _ := http.NewRequest("GET", "/debug/vrlog?timeout=100&limit=10", nil)
    30  	w := NewHTTPStreamWriterMock()
    31  
    32  	ch := vrLogStatsLogger.Subscribe("vrlogstats")
    33  	defer vrLogStatsLogger.Unsubscribe(ch)
    34  	go func() {
    35  		vrlogStatsHandler(ch, w, r)
    36  	}()
    37  	eventType, detail := "Test", "detail 1"
    38  	stats := NewVrLogStats(eventType)
    39  	stats.Send(detail)
    40  	var s string
    41  	select {
    42  	case ret := <-w.ch:
    43  		b, ok := ret.([]byte)
    44  		if ok {
    45  			s = string(b)
    46  		}
    47  	case <-time.After(1 * time.Second):
    48  		s = "Timed out"
    49  	}
    50  
    51  	want := fmt.Sprintf("%s Event	%s", eventType, detail)
    52  	if !strings.Contains(s, want) {
    53  		t.Fatalf(fmt.Sprintf("want %s, got %s", want, s))
    54  	}
    55  	if strings.HasSuffix(s, "\\n") {
    56  		t.Fatalf("does not end in a newline: %s", s)
    57  	}
    58  	s = strings.TrimSuffix(s, "\n")
    59  	ss := strings.Split(s, "	")
    60  	numCols := 4
    61  	if ss == nil || len(ss) != numCols {
    62  		t.Fatalf("log line should have %d columns, not %d, : %s", numCols, len(ss), strings.Join(ss, "|"))
    63  	}
    64  	lastColValue, err := strconv.Atoi(ss[len(ss)-1])
    65  	if err != nil {
    66  		t.Fatalf("Duration is not an integer: %s", err)
    67  	}
    68  	if lastColValue == 0 {
    69  		t.Fatalf("Duration should not be zero")
    70  	}
    71  
    72  	stats = &VrLogStats{}
    73  	stats.Send("detail123")
    74  
    75  	select {
    76  	case ret := <-w.ch:
    77  		b, ok := ret.([]byte)
    78  		if ok {
    79  			s = string(b)
    80  		}
    81  	case <-time.After(1 * time.Second):
    82  		s = "Timed out"
    83  	}
    84  	prefix := "Error: Type not specified"
    85  	if !strings.HasPrefix(s, prefix) {
    86  		t.Fatalf("Incorrect Type for uninitialized stat, got %v", s)
    87  	}
    88  }