ducky/devices

Paddy 2015-11-14 Parent:b6494e1a499e

5:408abf6e48d3 Go to Latest

ducky/devices/vendor/code.google.com/p/go-uuid/uuid/version1.go

Add more interface tests. Add a test to ensure that, when retrieving Devices, no error is returned if a Device cannot be found. Add a test to ensure that, when adding Devices, adding a Device that shares an ID with a Device already in the Storer returns an ErrDeviceAlreadyExists error. This involved creating the ErrDeviceAlreadyExists error, and modifying the in-memory implementation to properly return it. Fix a go vet issue in our previous test, wherein we forgot to pass the storer to a log message, resulting in a mismatch between the number of variables expected and the number of variables provided. Rename our tests to be better reflective of what they actually test.

History
paddy@0 1 // Copyright 2011 Google Inc. All rights reserved.
paddy@0 2 // Use of this source code is governed by a BSD-style
paddy@0 3 // license that can be found in the LICENSE file.
paddy@0 4
paddy@0 5 package uuid
paddy@0 6
paddy@0 7 import (
paddy@0 8 "encoding/binary"
paddy@0 9 )
paddy@0 10
paddy@0 11 // NewUUID returns a Version 1 UUID based on the current NodeID and clock
paddy@0 12 // sequence, and the current time. If the NodeID has not been set by SetNodeID
paddy@0 13 // or SetNodeInterface then it will be set automatically. If the NodeID cannot
paddy@0 14 // be set NewUUID returns nil. If clock sequence has not been set by
paddy@0 15 // SetClockSequence then it will be set automatically. If GetTime fails to
paddy@0 16 // return the current NewUUID returns nil.
paddy@0 17 func NewUUID() UUID {
paddy@0 18 if nodeID == nil {
paddy@0 19 SetNodeInterface("")
paddy@0 20 }
paddy@0 21
paddy@0 22 now, seq, err := GetTime()
paddy@0 23 if err != nil {
paddy@0 24 return nil
paddy@0 25 }
paddy@0 26
paddy@0 27 uuid := make([]byte, 16)
paddy@0 28
paddy@0 29 time_low := uint32(now & 0xffffffff)
paddy@0 30 time_mid := uint16((now >> 32) & 0xffff)
paddy@0 31 time_hi := uint16((now >> 48) & 0x0fff)
paddy@0 32 time_hi |= 0x1000 // Version 1
paddy@0 33
paddy@0 34 binary.BigEndian.PutUint32(uuid[0:], time_low)
paddy@0 35 binary.BigEndian.PutUint16(uuid[4:], time_mid)
paddy@0 36 binary.BigEndian.PutUint16(uuid[6:], time_hi)
paddy@0 37 binary.BigEndian.PutUint16(uuid[8:], seq)
paddy@0 38 copy(uuid[10:], nodeID)
paddy@0 39
paddy@0 40 return uuid
paddy@0 41 }