github.com/phuslu/log@v1.0.100/xid_test.go (about)

     1  package log
     2  
     3  import (
     4  	"encoding"
     5  	"encoding/json"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestXIDParse(t *testing.T) {
    11  	_, err := ParseXID("ab")
    12  	if err == nil {
    13  		t.Errorf("ParseXID should error")
    14  	}
    15  
    16  	_, err = ParseXID("\x012345678901234567890")
    17  	if err == nil {
    18  		t.Errorf("ParseXID should error")
    19  	}
    20  
    21  	for i := 0; i < 10; i++ {
    22  		x := NewXID()
    23  		got, _ := ParseXID(x.String())
    24  		want := x
    25  		if got != want {
    26  			t.Errorf("ParseXID(x) want=%+v got=%+v", want, got)
    27  		}
    28  	}
    29  }
    30  
    31  func TestXIDTime(t *testing.T) {
    32  	x := NewXID()
    33  	time.Sleep(1 * time.Second)
    34  	y := NewXID()
    35  
    36  	if y.Time().Sub(x.Time()) != 1*time.Second {
    37  		t.Errorf("XID.Time not correct")
    38  	}
    39  
    40  	if string(x.Machine()) != string(y.Machine()) {
    41  		t.Errorf("XID.Machine not correct")
    42  	}
    43  
    44  	if x.Pid() != y.Pid() {
    45  		t.Errorf("XID.Pid not correct")
    46  	}
    47  
    48  	if y.Counter()-x.Counter() != 1 {
    49  		t.Errorf("XID.Counter not correct")
    50  	}
    51  }
    52  
    53  func TestXIDMarshalJSON(t *testing.T) {
    54  	s := struct {
    55  		XID XID `json:"id"`
    56  	}{}
    57  	copy(s.XID[:], "012345678912")
    58  
    59  	data, err := json.Marshal(s)
    60  	if err != nil {
    61  		t.Errorf("json.Marshal(s) err: %+v", err)
    62  	}
    63  
    64  	got := string(data)
    65  	want := `{"id":"60oj4cpk6kr3ee1p64p0"}`
    66  	if got != want {
    67  		t.Errorf("json.Marshal(s) want=%+v got=%+v", want, got)
    68  	}
    69  
    70  	err = json.Unmarshal(data, &s)
    71  	if err != nil {
    72  		t.Errorf("json.Marshal(s) err: %+v", err)
    73  	}
    74  
    75  	got = string(s.XID[:])
    76  	want = "012345678912"
    77  	if got != want {
    78  		t.Errorf("json.Marshal(s) want=%#v got=%#v", want, got)
    79  	}
    80  }
    81  
    82  func TestXIDMarshalJSONNull(t *testing.T) {
    83  	s := struct {
    84  		XID XID `json:"id"`
    85  	}{}
    86  
    87  	data, err := json.Marshal(s)
    88  	if err != nil {
    89  		t.Errorf("json.Marshal(s) err: %+v", err)
    90  	}
    91  
    92  	got := string(data)
    93  	want := `{"id":null}`
    94  	if got != want {
    95  		t.Errorf("json.Marshal(s) want=%+v got=%+v", want, got)
    96  	}
    97  
    98  	err = json.Unmarshal(data, &s)
    99  	if err != nil {
   100  		t.Errorf("json.Marshal(s) err: %+v", err)
   101  	}
   102  
   103  	got = string(s.XID[:])
   104  	want = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
   105  	if got != want {
   106  		t.Errorf("json.Marshal(s) want=%#v got=%#v", want, got)
   107  	}
   108  }
   109  
   110  func TestXIDMarshalText(t *testing.T) {
   111  	x := NewXID()
   112  
   113  	var m encoding.TextMarshaler = x
   114  	text, err := m.MarshalText()
   115  	if err != nil {
   116  		t.Errorf("xid.MarshalText() err: %+v", err)
   117  	}
   118  
   119  	var y XID
   120  	var u encoding.TextUnmarshaler = &y
   121  	err = u.UnmarshalText(text)
   122  	if err != nil {
   123  		t.Errorf("xid.UnmarshalText() err: %+v", err)
   124  	}
   125  
   126  	if x != y {
   127  		t.Error("MarshalText()/UnmarshalText mismatched")
   128  	}
   129  }