Created
November 12, 2017 22:38
-
-
Save trickkiste/74de7d0f08c40c12eba4177dcd7845ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/go/src/test-worker-ffmpeg # go version | |
go version go1.8.4 linux/amd64 | |
/go/src/test-worker-ffmpeg # go test | |
scannerStderr | |
scannerStdout | |
writerStdin | |
----------------- 1. Iteration ----------------- | |
Empty message sent: | |
sending message | |
Response message received: | |
{"log_level":"error","created_at":"2017-11-12T22:38:05.194Z","log_message":"Unexpected end of JSON input\nSyntaxError: Unexpected end of JSON input\n at JSON.parse (<anonymous>)\n at JsonPipe.handleMessage (/usr/src/app/src/lib/jsonpipe.js:143:28)\n at emitOne (events.js:115:13)\n at Interface.emit (events.js:210:7)\n at Interface._onLine (readline.js:279:10)\n at Interface._normalWrite (readline.js:421:12)\n at Socket.ondata (readline.js:139:10)\n at emitOne (events.js:115:13)\n at Socket.emit (events.js:210:7)\n at addChunk (_stream_readable.js:266:12)"} | |
----------------- 2. Iteration ----------------- | |
Empty message sent: | |
sending message | |
Response message received: | |
{"log_level":"error","created_at":"2017-11-12T22:38:05.195Z","log_message":"Unexpected end of JSON input\nSyntaxError: Unexpected end of JSON input\n at JSON.parse (<anonymous>)\n at JsonPipe.handleMessage (/usr/src/app/src/lib/jsonpipe.js:143:28)\n at emitOne (events.js:115:13)\n at Interface.emit (events.js:210:7)\n at Interface._onLine (readline.js:279:10)\n at Interface._normalWrite (readline.js:421:12)\n at Socket.ondata (readline.js:139:10)\n at emitOne (events.js:115:13)\n at Socket.emit (events.js:210:7)\n at addChunk (_stream_readable.js:266:12)"} | |
----------------- 3. Iteration ----------------- | |
Empty message sent: | |
sending message | |
Response message received: | |
{"log_level":"error","created_at":"2017-11-12T22:38:05.196Z","log_message":"Unexpected end of JSON input\nSyntaxError: Unexpected end of JSON input\n at JSON.parse (<anonymous>)\n at JsonPipe.handleMessage (/usr/src/app/src/lib/jsonpipe.js:143:28)\n at emitOne (events.js:115:13)\n at Interface.emit (events.js:210:7)\n at Interface._onLine (readline.js:279:10)\n at Interface._normalWrite (readline.js:421:12)\n at Socket.ondata (readline.js:139:10)\n at emitOne (events.js:115:13)\n at Socket.emit (events.js:210:7)\n at addChunk (_stream_readable.js:266:12)"} | |
received done signal true | |
process killed. | |
scannerStdout | |
scannerStderr | |
writerStdin | |
----------------- 1. Iteration ----------------- | |
Message sent: | |
{"topic":"namespace_listener/tick","service_name":"testerName","service_host":"cb5117244501","service_uuid":"58bdb16a-7151-4762-af9c-20b98e78b49b","created_at":"2017-11-12T22:38:05.197831831Z","payload":{"uuid":"9978a96a-6ec2-40c4-8c00-ec9e8149199c"}} | |
sending message | |
----------------- 2. Iteration ----------------- | |
Message sent: | |
{"topic":"namespace_listener/tick","service_name":"testerName","service_host":"cb5117244501","service_uuid":"58bdb16a-7151-4762-af9c-20b98e78b49b","created_at":"2017-11-12T22:38:05.197831831Z","payload":{"uuid":"9978a96a-6ec2-40c4-8c00-ec9e8149199c"}} | |
sending message | |
----------------- 3. Iteration ----------------- | |
Message sent: | |
{"topic":"namespace_listener/tick","service_name":"testerName","service_host":"cb5117244501","service_uuid":"58bdb16a-7151-4762-af9c-20b98e78b49b","created_at":"2017-11-12T22:38:05.197831831Z","payload":{"uuid":"9978a96a-6ec2-40c4-8c00-ec9e8149199c"}} | |
sending message | |
--- FAIL: TestRepeatedTickResponseWhenIdle (0.12s) | |
Error Trace: main_test.go:180 | |
Error: Not equal: | |
expected: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", ServiceName:"worker-ffmpeg", ServiceHost:"cb5117244501", ServiceUUID:"f3a728ff-2e8e-48d7-874a-d08b3026586b", CreatedAt:time.Time{sec:63646123085, nsec:309000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x99, 0x78, 0xa9, 0x6a, 0x6e, 0xc2, 0x40, 0xc4, 0x8c, 0x0, 0xec, 0x9e, 0x81, 0x49, 0x19, 0x9c}, TickTimeStamp:time.Time{sec:63646123085, nsec:197831831, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
actual: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"namespace_publisher/job_queue/workers/undefined/worker_idle", ServiceName:"worker-ffmpeg", ServiceHost:"", ServiceUUID:"", CreatedAt:time.Time{sec:63646123085, nsec:309000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x99, 0x78, 0xa9, 0x6a, 0x6e, 0xc2, 0x40, 0xc4, 0x8c, 0x0, 0xec, 0x9e, 0x81, 0x49, 0x19, 0x9c}, TickTimeStamp:time.Time{sec:63646123085, nsec:197831831, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
Diff: | |
--- Expected | |
+++ Actual | |
@@ -2,6 +2,6 @@ | |
MessageHeader: (main.MessageHeader) { | |
- Topic: (string) (len=86) "namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", | |
+ Topic: (string) (len=59) "namespace_publisher/job_queue/workers/undefined/worker_idle", | |
ServiceName: (string) (len=13) "worker-ffmpeg", | |
- ServiceHost: (string) (len=12) "cb5117244501", | |
- ServiceUUID: (string) (len=36) "f3a728ff-2e8e-48d7-874a-d08b3026586b", | |
+ ServiceHost: (string) "", | |
+ ServiceUUID: (string) "", | |
CreatedAt: (time.Time) 2017-11-12 22:38:05.309 +0000 UTC | |
Messages: Unexpected worker_idle message object and/or values | |
Error Trace: main_test.go:180 | |
Error: Not equal: | |
expected: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", ServiceName:"worker-ffmpeg", ServiceHost:"cb5117244501", ServiceUUID:"f3a728ff-2e8e-48d7-874a-d08b3026586b", CreatedAt:time.Time{sec:63646123085, nsec:311000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x99, 0x78, 0xa9, 0x6a, 0x6e, 0xc2, 0x40, 0xc4, 0x8c, 0x0, 0xec, 0x9e, 0x81, 0x49, 0x19, 0x9c}, TickTimeStamp:time.Time{sec:63646123085, nsec:197831831, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
actual: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"", ServiceName:"", ServiceHost:"", ServiceUUID:"", CreatedAt:time.Time{sec:63646123085, nsec:311000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, TickTimeStamp:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
Diff: | |
--- Expected | |
+++ Actual | |
@@ -2,6 +2,6 @@ | |
MessageHeader: (main.MessageHeader) { | |
- Topic: (string) (len=86) "namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", | |
- ServiceName: (string) (len=13) "worker-ffmpeg", | |
- ServiceHost: (string) (len=12) "cb5117244501", | |
- ServiceUUID: (string) (len=36) "f3a728ff-2e8e-48d7-874a-d08b3026586b", | |
+ Topic: (string) "", | |
+ ServiceName: (string) "", | |
+ ServiceHost: (string) "", | |
+ ServiceUUID: (string) "", | |
CreatedAt: (time.Time) 2017-11-12 22:38:05.311 +0000 UTC | |
@@ -9,4 +9,4 @@ | |
TickReference: (main.TickReference) { | |
- TickUUID: (uuid.UUID) (len=16) 9978a96a-6ec2-40c4-8c00-ec9e8149199c, | |
- TickTimeStamp: (time.Time) 2017-11-12 22:38:05.197831831 +0000 UTC | |
+ TickUUID: (uuid.UUID) (len=16) 00000000-0000-0000-0000-000000000000, | |
+ TickTimeStamp: (time.Time) 0001-01-01 00:00:00 +0000 UTC | |
}, | |
Messages: Unexpected worker_idle message object and/or values | |
Error Trace: main_test.go:180 | |
Error: Not equal: | |
expected: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", ServiceName:"worker-ffmpeg", ServiceHost:"cb5117244501", ServiceUUID:"f3a728ff-2e8e-48d7-874a-d08b3026586b", CreatedAt:time.Time{sec:63646123085, nsec:312000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x99, 0x78, 0xa9, 0x6a, 0x6e, 0xc2, 0x40, 0xc4, 0x8c, 0x0, 0xec, 0x9e, 0x81, 0x49, 0x19, 0x9c}, TickTimeStamp:time.Time{sec:63646123085, nsec:197831831, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
actual: main.WorkerIdleSchema{MessageHeader:main.MessageHeader{Topic:"", ServiceName:"", ServiceHost:"", ServiceUUID:"", CreatedAt:time.Time{sec:63646123085, nsec:312000000, loc:(*time.Location)(nil)}}, TickReference:main.TickReference{TickUUID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, TickTimeStamp:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}}, Payload:struct {}{}} | |
Diff: | |
--- Expected | |
+++ Actual | |
@@ -2,6 +2,6 @@ | |
MessageHeader: (main.MessageHeader) { | |
- Topic: (string) (len=86) "namespace_publisher/job_queue/workers/f3a728ff-2e8e-48d7-874a-d08b3026586b/worker_idle", | |
- ServiceName: (string) (len=13) "worker-ffmpeg", | |
- ServiceHost: (string) (len=12) "cb5117244501", | |
- ServiceUUID: (string) (len=36) "f3a728ff-2e8e-48d7-874a-d08b3026586b", | |
+ Topic: (string) "", | |
+ ServiceName: (string) "", | |
+ ServiceHost: (string) "", | |
+ ServiceUUID: (string) "", | |
CreatedAt: (time.Time) 2017-11-12 22:38:05.312 +0000 UTC | |
@@ -9,4 +9,4 @@ | |
TickReference: (main.TickReference) { | |
- TickUUID: (uuid.UUID) (len=16) 9978a96a-6ec2-40c4-8c00-ec9e8149199c, | |
- TickTimeStamp: (time.Time) 2017-11-12 22:38:05.197831831 +0000 UTC | |
+ TickUUID: (uuid.UUID) (len=16) 00000000-0000-0000-0000-000000000000, | |
+ TickTimeStamp: (time.Time) 0001-01-01 00:00:00 +0000 UTC | |
}, | |
Messages: Unexpected worker_idle message object and/or values | |
FAIL | |
exit status 1 | |
FAIL test-worker-ffmpeg 0.237s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sometimes our Go programs need to spawn other, non-Go | |
// processes. For example, the syntax highlighting on this | |
// site is [implemented](https://github.com/mmcgrana/gobyexample/blob/master/tools/generate.go) | |
// by spawning a [`pygmentize`](http://pygments.org/) | |
// process from a Go program. Let's look at a few examples | |
// of spawning processes from Go. | |
package main | |
//import "fmt" | |
//import "io/ioutil" | |
//import "os" | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/satori/go.uuid" | |
"github.com/stretchr/testify/assert" | |
"os" | |
"os/exec" | |
"testing" | |
//"strconv" | |
"time" | |
//"io" | |
"bufio" | |
"flag" | |
// "errors" | |
//"log" | |
) | |
type MessageHeader struct { | |
Topic string `json:"topic"` | |
ServiceName string `json:"service_name"` | |
ServiceHost string `json:"service_host"` | |
ServiceUUID string `json:"service_uuid"` | |
CreatedAt time.Time `json:"created_at"` | |
} | |
type TickReference struct { | |
TickUUID uuid.UUID `json:"uuid,omitempty"` | |
TickTimeStamp time.Time `json:"created_at,omitempty"` | |
} | |
type TickPayload struct { | |
TickUUID uuid.UUID `json:"uuid"` | |
} | |
type TickSchema struct { | |
MessageHeader | |
Payload TickPayload `json:"payload"` | |
} | |
type TickResponseSchema struct { | |
MessageHeader | |
TickReference TickReference `json:"tick_reference,omitempty"` | |
} | |
type WorkerIdleSchema struct { | |
MessageHeader | |
TickReference TickReference `json:"tick_reference,omitempty"` | |
Payload struct{} `json:"payload,omitempty"` | |
} | |
type LogSchema struct { | |
LogLevel string `json:"log_level"` | |
CreatedAt time.Time `json:"created_at"` | |
LogMessage string `json:"log_message"` | |
} | |
//var Message MessageSchema | |
var testCmd string | |
var testArgs []string | |
var ServiceUUID string | |
var ServiceName string | |
var ServiceHost string | |
var NamespaceListener string | |
var NamespacePublisher string | |
var TesterUUID string = uuid.NewV4().String() | |
var TesterName string = "testerName" | |
func TestMain(m *testing.M) { | |
// flag.Parse() | |
// fmt.Println(flag.NArg(), flag.Args()) | |
if ServiceUUID = os.Getenv("SERVICE_UUID"); ServiceUUID == "" { | |
ServiceUUID = uuid.NewV4().String() | |
} | |
if ServiceName = os.Getenv("SERVICE_NAME"); ServiceName == "" { | |
ServiceName = "service_name" | |
} | |
if ServiceHost = os.Getenv("SERVICE_HOST"); ServiceHost == "" { | |
if ServiceHost, _ = os.Hostname(); ServiceHost == "" { | |
ServiceHost = "hostname" | |
} | |
} | |
if NamespaceListener = os.Getenv("NAMESPACE_LISTENER"); NamespaceListener == "" { | |
NamespaceListener = "namespace_listener" | |
} | |
if NamespacePublisher = os.Getenv("NAMESPACE_PUBLISHER"); NamespacePublisher == "" { | |
NamespacePublisher = "namespace_publisher" | |
} | |
result := m.Run() | |
os.Exit(result) | |
} | |
func TestEmptyLine(t *testing.T) { | |
input, output, done := runCore() | |
for i := 1; i <= 3; i++ { | |
fmt.Printf("----------------- %v. Iteration -----------------\n", i) | |
input <- []byte("\n") | |
fmt.Println("Empty message sent:\n") | |
response := <-output | |
var responseObj LogSchema | |
err := json.Unmarshal(response, &responseObj) | |
fmt.Println("Response message received:\n", string(response)) | |
assert.Equal(t, nil, err, "Expected log_message") | |
/* responseJson, _ := json.Marshal(&responseObj) | |
fmt.Println("Response message after parsing:\n", string(responseJson)) | |
r.CreatedAt = responseObj.CreatedAt | |
assert.Exactly(t, r, responseObj, "Unexpected worker_idle message object and/or values") | |
*/ | |
} | |
done <- true | |
} | |
func TestRepeatedTickResponseWhenIdle(t *testing.T) { | |
input, output, done := runCore() | |
m := TickSchema{ | |
MessageHeader{ | |
ServiceUUID: TesterUUID, | |
ServiceName: TesterName, | |
ServiceHost: ServiceHost, | |
Topic: NamespaceListener + "/tick", | |
CreatedAt: time.Now(), | |
}, | |
TickPayload{ | |
TickUUID: uuid.NewV4(), | |
}, | |
} | |
r := WorkerIdleSchema{ | |
MessageHeader{ | |
ServiceUUID: ServiceUUID, | |
ServiceName: ServiceName, | |
ServiceHost: ServiceHost, | |
Topic: NamespacePublisher + "/job_queue/workers/" + ServiceUUID + "/worker_idle", | |
CreatedAt: time.Now(), | |
}, | |
TickReference{ | |
TickUUID: m.Payload.TickUUID, | |
TickTimeStamp: m.CreatedAt.UTC(), | |
}, | |
struct{}{}, | |
} | |
for i := 1; i <= 3; i++ { | |
fmt.Printf("----------------- %v. Iteration -----------------\n", i) | |
mJson, _ := json.Marshal(m) | |
input <- mJson | |
fmt.Println("Message sent:\n", string(mJson)) | |
response := <-output | |
var responseObj WorkerIdleSchema | |
err := json.Unmarshal(response, &responseObj) | |
//fmt.Println("Response message received:\n", string(response)) | |
//responseJson, _ := json.Marshal(&responseObj) | |
//fmt.Println("Response message after parsing:\n", string(responseJson)) | |
assert.Equal(t, nil, err, "JSON parsing error when converting worker_idle message") | |
r.CreatedAt = responseObj.CreatedAt | |
assert.Exactly(t, r, responseObj, "Unexpected worker_idle message object and/or values") | |
} | |
done <- true | |
} | |
func runCore() (chan []byte, chan []byte, chan bool) { | |
switch flagArgs := flag.Args(); flag.NArg() { | |
case 0: | |
testCmd = "node" | |
testArgs = []string{"/usr/src/app/src/worker.js"} | |
case 1: | |
testCmd = flag.Arg(0) | |
testArgs = nil | |
default: | |
testCmd = flag.Arg(0) | |
testArgs = flagArgs[1:] | |
} | |
in := make(chan []byte) | |
out := make(chan []byte) | |
done := make(chan bool) | |
testProcess := exec.Command(testCmd, testArgs...) | |
testProcess.Env = os.Environ() | |
testIn, _ := testProcess.StdinPipe() | |
testOut, _ := testProcess.StdoutPipe() | |
testErr, _ := testProcess.StderrPipe() | |
go func() { | |
fmt.Println("scannerStdout") | |
scannerStdout := bufio.NewScanner(testOut) | |
for scannerStdout.Scan() { | |
out <- scannerStdout.Bytes() | |
} | |
}() | |
go func() { | |
fmt.Println("scannerStderr") | |
scannerStderr := bufio.NewScanner(testErr) | |
for scannerStderr.Scan() { | |
out <- scannerStderr.Bytes() | |
} | |
}() | |
go func() { | |
fmt.Println("writerStdin") | |
for { | |
select { | |
case message := <-in: | |
fmt.Println("sending message") | |
message = append(message, "\n"...) | |
testIn.Write(message) | |
case sig := <-done: | |
fmt.Println("received done signal", sig) | |
testIn.Close() | |
testOut.Close() | |
testErr.Close() | |
if err := testProcess.Process.Kill(); err != nil { | |
//log.Fatal("failed to kill: ", err) | |
fmt.Println("failed to kill") | |
} else { | |
//log.Println("process killed.") | |
fmt.Println("process killed.") | |
} | |
//testProcess.Wait() | |
} | |
} | |
}() | |
testProcess.Start() | |
return in, out, done | |
} | |
/* Function to run the groutine to run for stdin read */ | |
/* | |
func read(r io.Reader) <-chan string { | |
lines := make(chan string) | |
go func() { | |
defer close(lines) | |
scan := bufio.NewScanner(r) | |
for scan.Scan() { | |
lines <- scan.Text() | |
} | |
}() | |
return lines | |
} | |
func main() { | |
mes := read(os.Stdin) //Reading from Stdin | |
for anu := range mes { | |
fmt.Println("Message to stdout") | |
fmt.Println(anu) //Writing to Stdout | |
} | |
} | |
f | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment