github.com/getgauge/gauge@v1.6.9/logger/customWriter_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package logger
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestCustomWriterShouldExtractPortNumberFromStdout(t *testing.T) {
    16  	portChan := make(chan string)
    17  	var b bytes.Buffer
    18  	w := CustomWriter{
    19  		file: &b,
    20  		port: portChan,
    21  	}
    22  
    23  	go func() {
    24  		n, err := w.Write([]byte("Listening on port:23454"))
    25  		if n <= 0 || err != nil {
    26  			t.Errorf("failed to write port information")
    27  		}
    28  	}()
    29  
    30  	select {
    31  	case port := <-portChan:
    32  		close(portChan)
    33  		if port != "23454" {
    34  			t.Errorf("Expected:%s\nGot     :%s", "23454", port)
    35  		}
    36  	case <-time.After(3 * time.Second):
    37  		t.Errorf("Timed out!! Failed to get port info.")
    38  	}
    39  }
    40  
    41  func TestCustomWriterShouldExtractPortNumberFromStdoutWithMultipleLines(t *testing.T) {
    42  	portChan := make(chan string)
    43  	var b bytes.Buffer
    44  	w := CustomWriter{
    45  		file: &b,
    46  		port: portChan,
    47  	}
    48  
    49  	go func() {
    50  		s := `{"logLevel": "debug", "message": "Loading step implementations from spec0_3389569547211323752\step_impl dirs."}
    51  {"logLevel": "debug", "message": "Starting grpc server.."}
    52  {"logLevel": "info", "message": "Listening on port:50042"}`
    53  		n, err := w.Write([]byte(s))
    54  		if n <= 0 || err != nil {
    55  			t.Errorf("failed to write port information")
    56  		}
    57  	}()
    58  
    59  	select {
    60  	case port := <-portChan:
    61  		close(portChan)
    62  		if port != "50042" {
    63  			t.Errorf("Expected:%s\nGot     :%s", "50042", port)
    64  		}
    65  	case <-time.After(3 * time.Second):
    66  		t.Errorf("Timed out!! Failed to get port info.")
    67  	}
    68  }