File: time.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (162 lines) | stat: -rw-r--r-- 4,532 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# time.test = Copyright (C) 2003 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Exercise the tcllib time package.
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: time.test,v 1.12 2006/10/09 21:41:41 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal time.tcl time
}

# -------------------------------------------------------------------------
# Constraints
#
tcltest::testConstraint remote 0; #  set true to use the remote tests.
tcltest::testConstraint udp \
        [llength [concat \
        [package provide udp] \
        [package provide ceptcl]]]

# -------------------------------------------------------------------------

set testScript tstsrv.tmp

proc createServerProcess {} {
    file delete -force $::testScript
    set f [open $::testScript w+]
    puts $f {
        # This proc is called to handle client connections. We only need to
        # return the time in TIME epoch and then close the channel.
        proc ::srv {chan args} {
            fconfigure $chan -translation binary -buffering none -eofchar {}

            if {[catch {
                set r [binary format I [expr {int([clock seconds] + 2208988800)}]]
                puts "connect on $chan from [fconfigure $chan -peername]"
                puts -nonewline $chan $r
                close $chan
            } msg]} {
                puts stderr "error: $msg"
            }
            set ::done 1
        }
        
        set s [socket -server ::srv 0]
        fconfigure $s -translation binary -buffering none -eofchar {}
        set port [lindex [fconfigure $s -sockname] 2]
        
        puts $port 
        flush stdout
        vwait ::done
        update
        exit
    }
    close $f

    # Now run the server script as a child process - return child's
    # stdout to the caller so they can read the port to use.
    if {[catch {
        set f [open |[list [::tcltest::interpreter] $::testScript] r]
    }]} {
        set f [open |[list [info nameofexecutable] $::testScript] r]
    }

    fconfigure $f -buffering line -blocking 1
    #after 500 {set _init 1} ; vwait _init
    return $f
}

# -------------------------------------------------------------------------

set token {}

test time-1.1 {time::gettime} {
    global token
    list [catch {
        set f [createServerProcess]
        gets $f port
        set token [::time::gettime -protocol tcp localhost $port]
        set r {}
    } msg] $msg    
} {0 {}}

test time-1.2 {time::status} {
    global token
    list [catch {time::status $token} m] $m
} {0 ok}

test time-1.3 {time::unixtime} {
    global token
    list [catch {
        set t [time::unixtime $token]
        expr {(0 <= $t) && ($t <= 2147483647)}
    } m] $m
} {0 1}

test time-1.4 {time::cget} {
    global token
    list [catch {
        time::cget -port
    } m] $m
} {0 37}

test time-1.5 {time::cleanup} {
    global token
    list [catch {
        time::cleanup $token
    } m] $m
} {0 {}}


# -------------------------------------------------------------------------

test time-2.0 {check for real: RFC 868} {remote} {
    set ::time::TestReady 0
    list [catch {
        set tok [time::gettime -protocol tcp -timeout 5000 ntp2a.mcc.ac.uk]
        time::wait $tok
        list [time::status $tok] [time::cleanup $tok]
    } err] $err
} {0 {ok {}}}

test time-2.1 {check for real: RFC 868} {remote udp} {
    set ::time::TestReady 0
    list [catch {
        set tok [time::gettime -protocol udp -timeout 5000 ntp2a.mcc.ac.uk]
        time::wait $tok
        list [time::status $tok] [time::cleanup $tok]
    } err] $err
} {0 {ok {}}}

test time-2.2 {check for real: RFC 2030} {remote udp} {
    set ::time::TestReady 0
    list [catch {
        set tok [time::getsntp -timeout 5000 ntp2a.mcc.ac.uk]
        time::wait $tok
        list [time::status $tok] [time::cleanup $tok]
    } err] $err
} {0 {ok {}}}

# -------------------------------------------------------------------------
file delete -force $::testScript
testsuiteCleanup
return

#
# Local variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End: