golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/ringlogger/cli_test.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package ringlogger
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"sync"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestThreads(t *testing.T) {
    17  	wg := sync.WaitGroup{}
    18  	wg.Add(2)
    19  	go func() {
    20  		rl, err := NewRinglogger("ringlogger_test.bin", "ONE")
    21  		if err != nil {
    22  			t.Fatal(err)
    23  		}
    24  		for i := 0; i < 1024; i++ {
    25  			fmt.Fprintf(rl, "bla bla bla %d", i)
    26  		}
    27  		rl.Close()
    28  		wg.Done()
    29  	}()
    30  	go func() {
    31  		rl, err := NewRinglogger("ringlogger_test.bin", "TWO")
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  		for i := 1024; i < 2047; i++ {
    36  			fmt.Fprintf(rl, "bla bla bla %d", i)
    37  		}
    38  		rl.Close()
    39  		wg.Done()
    40  	}()
    41  	wg.Wait()
    42  }
    43  
    44  func TestWriteText(t *testing.T) {
    45  	rl, err := NewRinglogger("ringlogger_test.bin", "TXT")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if len(os.Args) != 3 {
    50  		t.Fatal("Should pass exactly one argument")
    51  	}
    52  	fmt.Fprintf(rl, os.Args[2])
    53  	rl.Close()
    54  }
    55  
    56  func TestDump(t *testing.T) {
    57  	rl, err := NewRinglogger("ringlogger_test.bin", "DMP")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	_, err = rl.WriteTo(os.Stdout)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	rl.Close()
    66  }
    67  
    68  func TestFollow(t *testing.T) {
    69  	rl, err := NewRinglogger("ringlogger_test.bin", "FOL")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	cursor := CursorAll
    74  	for {
    75  		var lines []FollowLine
    76  		lines, cursor = rl.FollowFromCursor(cursor)
    77  		for _, line := range lines {
    78  			fmt.Printf("%v: %s\n", line.Stamp, line.Line)
    79  		}
    80  		time.Sleep(300 * time.Millisecond)
    81  	}
    82  }