diff --git a/errorkernel.go b/errorkernel.go index 4f9b944..dd0bd17 100644 --- a/errorkernel.go +++ b/errorkernel.go @@ -24,6 +24,8 @@ type errorKernel struct { // errorCh is used to report errors from a process errorCh chan errorEvent + // testCh is used within REQTest for receving data for tests. + testCh chan []byte ctx context.Context cancel context.CancelFunc @@ -36,6 +38,7 @@ func newErrorKernel(ctx context.Context, m *metrics) *errorKernel { return &errorKernel{ errorCh: make(chan errorEvent, 2), + testCh: make(chan []byte), ctx: ctxC, cancel: cancel, metrics: m, @@ -229,7 +232,7 @@ type errorAction int const ( // errActionContinue is ment to be used when the a process - // can just continue without takig any special care. + // can just continue without taking any special care. errActionContinue errorAction = iota // TODO: // errActionKill should log the error, diff --git a/processes.go b/processes.go index 8affffe..329602d 100644 --- a/processes.go +++ b/processes.go @@ -114,6 +114,13 @@ func (p *processes) Start(proc process) { go proc.spawnWorker() } + { + log.Printf("Starting REQTest subscriber: %#v\n", proc.node) + sub := newSubject(REQTest, string(proc.node)) + proc := newProcess(proc.ctx, p.server, sub, processKindSubscriber, nil) + go proc.spawnWorker() + } + if proc.configuration.StartSubREQToFileAppend { proc.startup.subREQToFileAppend(proc) } diff --git a/requests.go b/requests.go index 5a0e2a7..fae5f5c 100644 --- a/requests.go +++ b/requests.go @@ -118,6 +118,10 @@ const ( REQRelayInitial Method = "REQRelayInitial" // REQNone is used when there should be no reply. REQNone Method = "REQNone" + // REQTest is used only for testing to be able to grab the output + // of messages. + REQTest Method = "REQTest" + // REQPublicKey will get the public ed25519 key from a node. REQPublicKey Method = "REQPublicKey" // REQPublicKeysGet will get all the public keys from central. @@ -126,6 +130,7 @@ const ( REQPublicKeysToNode Method = "REQPublicKeysToNode" // REQAuthPublicKeysAllow REQPublicKeysAllow Method = "REQPublicKeysAllow" + // REQAclAddCommand REQAclAddCommand = "REQAclAddCommand" // REQAclDeleteCommand @@ -276,6 +281,9 @@ func (m Method) GetMethodsAvailable() MethodsAvailable { REQAclImport: methodREQAclImport{ event: EventACK, }, + REQTest: methodREQTest{ + event: EventACK, + }, }, } diff --git a/requests_std.go b/requests_std.go index b878850..c6b38d2 100644 --- a/requests_std.go +++ b/requests_std.go @@ -432,3 +432,32 @@ func (m methodREQTuiToConsole) handler(proc process, message Message, node strin } // --- + +type methodREQTest struct { + event Event +} + +func (m methodREQTest) getKind() Event { + return m.event +} + +// handler to be used as a reply method when testing requests. +// We can then within the test listen on the testCh for received +// data and validate it. +// If not test is listening the data will be dropped. +func (m methodREQTest) handler(proc process, message Message, node string) ([]byte, error) { + + go func() { + // Try to send the received message data on the test channel. If we + // have a test started the data will be read from the testCh. + // If no test is reading from the testCh the data will be dropped. + select { + case proc.errorKernel.testCh <- message.Data: + default: + // drop. + } + }() + + ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) + return ackMsg, nil +} diff --git a/requests_test.go b/requests_test.go new file mode 100644 index 0000000..6aaab10 --- /dev/null +++ b/requests_test.go @@ -0,0 +1,7 @@ +package steward + +func requestTest() { + type test struct { + want []byte + } +}