Skip to content

Commit

Permalink
Added unit test for includeInSample
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Jul 29, 2016
1 parent a2021e4 commit 13b2d14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/github.com/getlantern/flashlight/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,13 @@ func includeInSample(deviceID string, samplePercentage float64) bool {
log.Debugf("Error decoding base64 deviceID %v: %v", deviceID, base64Err)
return false
}
var deviceIDInt uint64
if len(deviceIDBytes) != 6 {
log.Debugf("Unexpected DeviceID length %v: %d", deviceID, len(deviceIDBytes))
return false
}
// Pad and decode to int
paddedDeviceIDBytes := append(deviceIDBytes, 0, 0)
// Use BigEndian because Mac address has most significant bytes on left
deviceIDInt = binary.BigEndian.Uint64(paddedDeviceIDBytes)
// Use LittleEndian because Mac address has most significant bytes on left
deviceIDInt := binary.LittleEndian.Uint64(paddedDeviceIDBytes)
return deviceIDInt%uint64(1/samplePercentage) == 0
}
14 changes: 14 additions & 0 deletions src/github.com/getlantern/flashlight/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package logging

import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"regexp"
Expand Down Expand Up @@ -112,3 +114,15 @@ func TestLoggly(t *testing.T) {
assert.Equal(t, 100, len(result["message"].(string)))
}
}

func TestIncludeInSample(t *testing.T) {
included := 0
b := make([]byte, 8)
for i := uint64(0); i < 100; i++ {
binary.LittleEndian.PutUint64(b, i)
if includeInSample(base64.StdEncoding.EncodeToString(b[:6]), 0.01) {
included++
}
}
assert.Equal(t, 1, included, "Only 1% should have been included")
}

0 comments on commit 13b2d14

Please sign in to comment.