github.com/MasterDimmy/zipologger@v0.3.8/empty_test.go (about)

     1  package zipologger
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  func init() {
    11  	os.Chdir("C:\\gopath\\src\\github.com\\MasterDimmy\\zipologger\\")
    12  }
    13  
    14  func remove_logs() {
    15  	err := os.Remove("./logs/a.log")
    16  	if err != nil && !strings.Contains(err.Error(), "The system cannot find the file") {
    17  		panic(err.Error())
    18  	}
    19  	err = os.Remove("./logs/b.log")
    20  	if err != nil && !strings.Contains(err.Error(), "The system cannot find the file") {
    21  		panic(err.Error())
    22  	}
    23  }
    24  
    25  func Test_wait(t *testing.T) {
    26  	SetAlsoToStdout(false)
    27  
    28  	remove_logs()
    29  
    30  	l1 := GetLoggerBySuffix("a.log", "./logs/", 1, 1, 1, true)
    31  	l2 := GetLoggerBySuffix("b.log", "./logs/", 1, 1, 1, true)
    32  
    33  	l1.Wait()
    34  	l1.Wait()
    35  	l2.Wait()
    36  	l1.Wait()
    37  
    38  	for i := 0; i < 100; i++ {
    39  		l2.Print("aaaa")
    40  		l1.Print("bbb")
    41  	}
    42  
    43  	Wait()
    44  
    45  	tf := func(fname string, cnt int) {
    46  		a1, err := os.ReadFile(fname)
    47  		if err != nil {
    48  			t.Fatal(err.Error())
    49  		}
    50  		astr := strings.TrimSpace(string(a1))
    51  		al := strings.Split(astr, "\n")
    52  		if len(al) != cnt {
    53  			t.Fatalf("%s not %d lines: %d\n", fname, cnt, len(al))
    54  		}
    55  	}
    56  
    57  	tf("./logs/a.log", 100)
    58  	tf("./logs/b.log", 100)
    59  
    60  	for i := 0; i < 100; i++ {
    61  		//t.Log("print 1")
    62  
    63  		l2.Print("aaaa")
    64  		l2.Flush()
    65  
    66  		l1.Print("bbb")
    67  
    68  		l1.Flush()
    69  		l2.Flush()
    70  		l1.Wait()
    71  		l2.Wait()
    72  		l1.Wait()
    73  		l1.Wait()
    74  	}
    75  
    76  	wg := sync.WaitGroup{}
    77  	for ii := 0; ii < 100; ii++ {
    78  		go func(i int) {
    79  			wg.Add(1)
    80  			go func(j int) {
    81  				l1.Printf("[%d]", j)
    82  				l2.Printf("[%d]", j)
    83  				l1.Printf("[%d]", j)
    84  				wg.Add(1)
    85  				go func() {
    86  					l2.Println("zzzz")
    87  					wg.Done()
    88  				}()
    89  				wg.Done()
    90  			}(i)
    91  		}(ii)
    92  
    93  	}
    94  
    95  	empty := EmptyLogger
    96  	empty.Print("te")
    97  	empty.Flush()
    98  	empty.Wait()
    99  
   100  	wg.Wait()
   101  
   102  	for i := 0; i < 100; i++ {
   103  		l1.Printf("zxswq [%d]", i)
   104  	}
   105  
   106  	for i := 0; i < 100; i++ {
   107  		l2.Printf("tgbf [%d]", i)
   108  	}
   109  
   110  	Wait()
   111  
   112  	tf("./logs/a.log", 500)
   113  	tf("./logs/b.log", 500)
   114  }