Skip to content

Commit

Permalink
Minor optimizations per lint results
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 3, 2020
1 parent b951070 commit b45bf09
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
56 changes: 26 additions & 30 deletions log_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package logger

import (
"bytes"
"fmt"
"os"
"os/exec"
"testing"
"time"
)

const testToken = "token"

// TestNewLogEntriesClient will test the NewLogEntriesClient() method
func TestNewLogEntriesClient(t *testing.T) {
token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand All @@ -25,8 +25,8 @@ func TestNewLogEntriesClient(t *testing.T) {
t.Fatalf("[%s] expect [%s] result", LogEntriesURL, client.endpoint)
}

if client.token != token {
t.Fatalf("[%s] expect [%s] result", token, client.token)
if client.token != testToken {
t.Fatalf("[%s] expect [%s] result", testToken, client.token)
}

_, err = NewLogEntriesClient("token", LogEntriesURL, "101010")
Expand All @@ -40,7 +40,7 @@ func TestNewLogEntriesClient(t *testing.T) {
}

// Double open
client, err = NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
_, err = NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand All @@ -49,14 +49,13 @@ func TestNewLogEntriesClient(t *testing.T) {
// TestMsgQueue_Enqueue will test the Enqueue() method
func TestMsgQueue_Enqueue(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}

var buff bytes.Buffer
buff.WriteString(token)
buff.WriteString(testToken)
buff.WriteByte(' ')
buff.WriteString("test")
client.messages.Enqueue(&buff)
Expand All @@ -66,8 +65,8 @@ func TestMsgQueue_Enqueue(t *testing.T) {
}

for x := range client.messages.messagesToSend {
if fmt.Sprintf("%s", x) != token+" test" {
t.Fatalf("[%s] expect [%s] result", token+" test", fmt.Sprintf("%s", x))
if x.String() != testToken+" test" {
t.Fatalf("[%s] expect [%s] result", testToken+" test", x.String())
}
close(client.messages.messagesToSend)
}
Expand All @@ -76,20 +75,19 @@ func TestMsgQueue_Enqueue(t *testing.T) {
// TestMsgQueue_PushFront will test the PushFront() method
func TestMsgQueue_PushFront(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}

var buff bytes.Buffer
buff.WriteString(token)
buff.WriteString(testToken)
buff.WriteByte(' ')
buff.WriteString("test")
client.messages.Enqueue(&buff)

var buff2 bytes.Buffer
buff2.WriteString(token)
buff2.WriteString(testToken)
buff2.WriteByte(' ')
buff2.WriteString("first")
client.messages.PushFront(&buff2)
Expand All @@ -107,23 +105,23 @@ func TestMsgQueue_PushFront(t *testing.T) {
finalString += x.String()
}

if finalString != token+" first"+token+" test" {
t.Fatalf("[%s] expect [%s] result", token+" first"+token+" test", finalString)
if finalString != testToken+" first"+testToken+" test" {
t.Fatalf("[%s] expect [%s] result", testToken+" first"+testToken+" test", finalString)
}
}

// TestLogClient_ProcessQueue will test the ProcessQueue() method
func TestLogClient_ProcessQueue(t *testing.T) {
token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}

go client.ProcessQueue()

var buff bytes.Buffer
buff.WriteString(token)
buff.WriteString(testToken)
buff.WriteByte(' ')
buff.WriteString("test")
client.messages.Enqueue(&buff)
Expand All @@ -137,8 +135,8 @@ func TestLogClient_ProcessQueue(t *testing.T) {

// TestLogClient_Println will test the Println() method
func TestLogClient_Println(t *testing.T) {
token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand All @@ -152,8 +150,8 @@ func TestLogClient_Println(t *testing.T) {

// TestLogClient_Printf will test the Printf() method
func TestLogClient_Printf(t *testing.T) {
token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand All @@ -173,16 +171,15 @@ func TestLogClient_Printf(t *testing.T) {
finalString += x.String()
}

if finalString != token+" test 1" {
t.Fatalf("[%s] expect [%s] result", token+" test 1", finalString)
if finalString != testToken+" test 1" {
t.Fatalf("[%s] expect [%s] result", testToken+" test 1", finalString)
}
}

// TestLogClient_Fatalf will test the Fatalf() method
func TestLogClient_Fatalf(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand All @@ -203,8 +200,7 @@ func TestLogClient_Fatalf(t *testing.T) {
// TestLogClient_Fatalln will test the Fatalln() method
func TestLogClient_Fatalln(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
if err != nil {
t.Fatalf("error should have not occurred: %s", err.Error())
}
Expand Down
23 changes: 1 addition & 22 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestFileTagComponents(t *testing.T) {
}

// Test the part // todo: this number changes frequently, maybe this is not the best test?
if fileTagComps[2] != "1108" {
if fileTagComps[2] != "1127" {
t.Fatalf("expected component: %s, got: %s", "991", fileTagComps[2])
}

Expand Down Expand Up @@ -389,27 +389,6 @@ func BenchmarkLogPkg_Println(b *testing.B) {
}
}

// TestMakeParameter test making an error struct and MakeParameter() method
func TestMakeParameter(t *testing.T) {
param := MakeParameter("myKey", "myValue")
if param.Key() != "myKey" {
t.Fatalf("expected value: %s, got: %s", "myKey", param.Key())
}
if param.Value() != "myValue" {
t.Fatalf("expected value: %s, got: %s", "myValue", param.Value())
}
if param.String() != `{"key":"myKey","value":"myValue"}` {
t.Fatalf("expected value: %s, got: %s", `{"key":"myKey","value":"myValue"}`, param.String())
}
}

// BenchmarkMakeParameter benchmarks the MakeParameter() method
func BenchmarkMakeParameter(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = MakeParameter("myKey", "myValue")
}
}

// TestFatalf will test the Fatalf() method
func TestFatalf(t *testing.T) {

Expand Down
24 changes: 24 additions & 0 deletions parameters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package logger

import "testing"

// TestMakeParameter test making an error struct and MakeParameter() method
func TestMakeParameter(t *testing.T) {
param := MakeParameter("myKey", "myValue")
if param.Key() != "myKey" {
t.Fatalf("expected value: %s, got: %s", "myKey", param.Key())
}
if param.Value() != "myValue" {
t.Fatalf("expected value: %s, got: %s", "myValue", param.Value())
}
if param.String() != `{"key":"myKey","value":"myValue"}` {
t.Fatalf("expected value: %s, got: %s", `{"key":"myKey","value":"myValue"}`, param.String())
}
}

// BenchmarkMakeParameter benchmarks the MakeParameter() method
func BenchmarkMakeParameter(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = MakeParameter("myKey", "myValue")
}
}

0 comments on commit b45bf09

Please sign in to comment.