vitess.io/vitess@v0.16.2/go/vt/logutil/proto3_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 logutil
    18  
    19  import (
    20  	"math"
    21  	"testing"
    22  	"time"
    23  
    24  	"google.golang.org/protobuf/proto"
    25  
    26  	"vitess.io/vitess/go/vt/proto/vttime"
    27  )
    28  
    29  const (
    30  	// Seconds field of the earliest valid Timestamp.
    31  	// This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
    32  	minValidSeconds = -62135596800
    33  	// Seconds field just after the latest valid Timestamp.
    34  	// This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
    35  	maxValidSeconds = 253402300800
    36  )
    37  
    38  func utcDate(year, month, day int) time.Time {
    39  	return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
    40  }
    41  
    42  var tests = []struct {
    43  	pt *vttime.Time
    44  	t  time.Time
    45  }{
    46  	// The timestamp representing the Unix epoch date.
    47  	{pt: &vttime.Time{Seconds: 0, Nanoseconds: 0},
    48  		t: utcDate(1970, 1, 1)},
    49  
    50  	// The smallest representable timestamp with non-negative nanos.
    51  	{pt: &vttime.Time{Seconds: math.MinInt64, Nanoseconds: 0},
    52  		t: time.Unix(math.MinInt64, 0).UTC()},
    53  
    54  	// The earliest valid timestamp.
    55  	{pt: &vttime.Time{Seconds: minValidSeconds, Nanoseconds: 0},
    56  		t: utcDate(1, 1, 1)},
    57  
    58  	// The largest representable timestamp with nanos in range.
    59  	{pt: &vttime.Time{Seconds: math.MaxInt64, Nanoseconds: 1e9 - 1},
    60  		t: time.Unix(math.MaxInt64, 1e9-1).UTC()},
    61  
    62  	// The largest valid timestamp.
    63  	{pt: &vttime.Time{Seconds: maxValidSeconds - 1, Nanoseconds: 1e9 - 1},
    64  		t: time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
    65  
    66  	// The smallest invalid timestamp that is larger than the valid range.
    67  	{pt: &vttime.Time{Seconds: maxValidSeconds, Nanoseconds: 0},
    68  		t: time.Unix(maxValidSeconds, 0).UTC()},
    69  
    70  	// A date before the epoch.
    71  	{pt: &vttime.Time{Seconds: -281836800, Nanoseconds: 0},
    72  		t: utcDate(1961, 1, 26)},
    73  
    74  	// A date after the epoch.
    75  	{pt: &vttime.Time{Seconds: 1296000000, Nanoseconds: 0},
    76  		t: utcDate(2011, 1, 26)},
    77  
    78  	// A date after the epoch, in the middle of the day.
    79  	{pt: &vttime.Time{Seconds: 1296012345, Nanoseconds: 940483},
    80  		t: time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)},
    81  }
    82  
    83  func TestProtoToTime(t *testing.T) {
    84  	for i, s := range tests {
    85  		got := ProtoToTime(s.pt)
    86  		if got != s.t {
    87  			t.Errorf("ProtoToTime[%v](%v) = %v, want %v", i, s.pt, got, s.t)
    88  		}
    89  	}
    90  }
    91  
    92  func TestTimeToProto(t *testing.T) {
    93  	for i, s := range tests {
    94  		got := TimeToProto(s.t)
    95  		if !proto.Equal(got, s.pt) {
    96  			t.Errorf("TimeToProto[%v](%v) = %v, want %v", i, s.t, got, s.pt)
    97  		}
    98  	}
    99  }