1# Copyright 2015 gRPC authors. 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 15require 'spec_helper' 16 17TimeConsts = GRPC::Core::TimeConsts 18 19describe TimeConsts do 20 before(:each) do 21 @known_consts = [:ZERO, :INFINITE_FUTURE, :INFINITE_PAST].sort 22 end 23 24 it 'should have all the known types' do 25 expect(TimeConsts.constants.collect.sort).to eq(@known_consts) 26 end 27 28 describe '#to_time' do 29 it 'converts each constant to a Time' do 30 m = TimeConsts 31 m.constants.each do |c| 32 expect(m.const_get(c).to_time).to be_a(Time) 33 end 34 end 35 end 36end 37 38describe '#from_relative_time' do 39 it 'cannot handle arbitrary objects' do 40 expect { TimeConsts.from_relative_time(Object.new) }.to raise_error 41 end 42 43 it 'preserves TimeConsts' do 44 m = TimeConsts 45 m.constants.each do |c| 46 const = m.const_get(c) 47 expect(TimeConsts.from_relative_time(const)).to be(const) 48 end 49 end 50 51 it 'converts 0 to TimeConsts::ZERO' do 52 expect(TimeConsts.from_relative_time(0)).to eq(TimeConsts::ZERO) 53 end 54 55 it 'converts nil to TimeConsts::ZERO' do 56 expect(TimeConsts.from_relative_time(nil)).to eq(TimeConsts::ZERO) 57 end 58 59 it 'converts negative values to TimeConsts::INFINITE_FUTURE' do 60 [-1, -3.2, -1e6].each do |t| 61 y = TimeConsts.from_relative_time(t) 62 expect(y).to eq(TimeConsts::INFINITE_FUTURE) 63 end 64 end 65 66 it 'converts a positive value to an absolute time' do 67 epsilon = 1 68 [1, 3.2, 1e6].each do |t| 69 want = Time.now + t 70 abs = TimeConsts.from_relative_time(t) 71 expect(abs.to_f).to be_within(epsilon).of(want.to_f) 72 end 73 end 74end 75