V
V
vladikor2020-02-28 17:18:16
go
vladikor, 2020-02-28 17:18:16

How to organize interruption of infinite loops in tests?

I inherited a project written on GO (comet server) and now the task is to cover everything with integration tests.
Comet processes messages from channels (go channels) in an infinite loop, then processes these messages.
In general, how to succinctly organize the interruption of such a cycle in tests after all messages have been processed?

I have only one thought - this is the addition of a new channel, which will be responsible for interrupting the loop, but something like that ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2020-02-28
@vladikor

Close the channel in the test, and the function reading in the message loop should terminate gracefully.
Example:

data, ok := <-channel
if !ok {
    return
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question