github.com/coreos/mantle@v0.13.0/network/journal/entry_test.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package journal
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func TestEntryRealtime(t *testing.T) {
    23  	for _, testcase := range []struct {
    24  		name   string
    25  		entry  Entry
    26  		expect time.Time
    27  	}{{
    28  		name:   "zero",
    29  		entry:  Entry{},
    30  		expect: time.Time{},
    31  	}, {
    32  		name: "has_source",
    33  		entry: Entry{
    34  			FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"),
    35  			FIELD_REALTIME_TIMESTAMP:        []byte("1342540861416409"),
    36  		},
    37  		expect: time.Unix(1342540861, 416351000),
    38  	}, {
    39  		name: "no_source",
    40  		entry: Entry{
    41  			FIELD_REALTIME_TIMESTAMP: []byte("1342540861416409"),
    42  		},
    43  		expect: time.Unix(1342540861, 416409000),
    44  	}, {
    45  		name: "invalid_source",
    46  		entry: Entry{
    47  			FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("bob"),
    48  			FIELD_REALTIME_TIMESTAMP:        []byte("1342540861416409"),
    49  		},
    50  		expect: time.Time{},
    51  	}, {
    52  		name: "invalid_both",
    53  		entry: Entry{
    54  			FIELD_REALTIME_TIMESTAMP: []byte("bob"),
    55  		},
    56  		expect: time.Time{},
    57  	},
    58  	} {
    59  		t.Run(testcase.name, func(t *testing.T) {
    60  			result := testcase.entry.Realtime()
    61  			if result != testcase.expect {
    62  				t.Errorf("%s != %s", result, testcase.expect)
    63  			}
    64  		})
    65  	}
    66  }