github.com/coreos/mantle@v0.13.0/network/ntp/server_test.go (about)

     1  // Copyright 2015 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 ntp
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func TestNewServer(t *testing.T) {
    23  	s, err := NewServer(":0")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer s.Close()
    28  }
    29  
    30  func TestServerSetTime(t *testing.T) {
    31  	start := time.Date(2013, time.December, 10, 19, 22, 35, 0, time.UTC)
    32  	s := &Server{}
    33  	s.SetTime(start)
    34  	offset := time.Now().Add(s.offset).Sub(start)
    35  	if offset < time.Duration(0) || offset > time.Second {
    36  		t.Errorf("Server time off by %s, internal offest %s",
    37  			offset, s.offset)
    38  	}
    39  	s.SetTime(time.Time{})
    40  	if s.offset != time.Duration(0) {
    41  		t.Errorf("Server offset not zero: %s", s.offset)
    42  	}
    43  }
    44  
    45  func TestServerSetLeap(t *testing.T) {
    46  	leap := time.Date(2012, time.July, 1, 0, 0, 0, 0, time.UTC)
    47  	s := &Server{}
    48  	s.SetLeapSecond(leap, LEAP_ADD)
    49  	if s.leapTime != leap || s.leapType != LEAP_ADD {
    50  		t.Errorf("Wrong leap: %s %s", s.leapTime, s.leapType)
    51  	}
    52  }
    53  
    54  type update struct {
    55  	now time.Duration
    56  	off time.Duration
    57  	li  LeapIndicator
    58  }
    59  
    60  func TestServerUpdate(t *testing.T) {
    61  	leap := time.Date(2012, time.July, 1, 0, 0, 0, 0, time.UTC)
    62  	s := &Server{}
    63  
    64  	s.SetLeapSecond(leap, LEAP_ADD)
    65  	for _, u := range []update{
    66  		{-48 * time.Hour, 0, LEAP_NONE},
    67  		{-25 * time.Hour, 0, LEAP_NONE},
    68  		{-24 * time.Hour, 0, LEAP_ADD},
    69  		{-23 * time.Hour, 0, LEAP_ADD},
    70  		{-time.Hour, 0, LEAP_ADD},
    71  		{-time.Minute, 0, LEAP_ADD},
    72  		{-time.Second, 0, LEAP_ADD},
    73  		{-time.Millisecond, 0, LEAP_ADD},
    74  		{-time.Microsecond, 0, LEAP_ADD},
    75  		{-time.Nanosecond, 0, LEAP_ADD},
    76  		{0, -time.Second, LEAP_NONE},
    77  		{time.Nanosecond, -time.Second, LEAP_NONE},
    78  		{time.Microsecond, -time.Second, LEAP_NONE},
    79  		{time.Millisecond, -time.Second, LEAP_NONE},
    80  		{time.Second, -time.Second, LEAP_NONE},
    81  		{time.Minute, -time.Second, LEAP_NONE},
    82  		{time.Hour, -time.Second, LEAP_NONE},
    83  	} {
    84  		now := leap.Add(u.now)
    85  		off, li := s.UpdateOffset(leap.Add(u.now))
    86  		if off != u.off || li != u.li {
    87  			t.Errorf("Wrong update at %s: %s!=%s %s!=%s",
    88  				now, u.off, off, u.li, li)
    89  		}
    90  	}
    91  
    92  	s = &Server{}
    93  	s.SetLeapSecond(leap, LEAP_SUB)
    94  	for _, u := range []update{
    95  		{-48 * time.Hour, 0, LEAP_NONE},
    96  		{-25 * time.Hour, 0, LEAP_NONE},
    97  		{-24 * time.Hour, 0, LEAP_SUB},
    98  		{-23 * time.Hour, 0, LEAP_SUB},
    99  		{-time.Hour, 0, LEAP_SUB},
   100  		{-time.Minute, 0, LEAP_SUB},
   101  		{-time.Second - time.Second, 0, LEAP_SUB},
   102  		{-time.Second - time.Millisecond, 0, LEAP_SUB},
   103  		{-time.Second - time.Microsecond, 0, LEAP_SUB},
   104  		{-time.Second - time.Nanosecond, 0, LEAP_SUB},
   105  		{-time.Second, time.Second, LEAP_NONE},
   106  		{time.Nanosecond, time.Second, LEAP_NONE},
   107  		{time.Microsecond, time.Second, LEAP_NONE},
   108  		{time.Millisecond, time.Second, LEAP_NONE},
   109  		{time.Second, time.Second, LEAP_NONE},
   110  		{time.Minute, time.Second, LEAP_NONE},
   111  		{time.Hour, time.Second, LEAP_NONE},
   112  	} {
   113  		now := leap.Add(u.now)
   114  		off, li := s.UpdateOffset(leap.Add(u.now))
   115  		if off != u.off || li != u.li {
   116  			t.Errorf("Wrong update at %s: %s!=%s %s!=%s",
   117  				now, u.off, off, u.li, li)
   118  		}
   119  	}
   120  }