2022-01-06 08:35:44 -06:00
|
|
|
package database_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-14 10:30:26 -06:00
|
|
|
|
2022-03-25 16:07:45 -05:00
|
|
|
"github.com/coder/coder/coderd/database"
|
2022-01-06 08:35:44 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPubsubMemory(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
t.Run("Memory", func(t *testing.T) {
|
2022-01-29 12:21:16 -08:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2022-01-06 08:35:44 -06:00
|
|
|
pubsub := database.NewPubsubInMemory()
|
|
|
|
|
event := "test"
|
|
|
|
|
data := "testing"
|
2022-01-20 10:00:13 -06:00
|
|
|
messageChannel := make(chan []byte)
|
2022-01-06 08:35:44 -06:00
|
|
|
cancelFunc, err := pubsub.Subscribe(event, func(ctx context.Context, message []byte) {
|
2022-01-20 10:00:13 -06:00
|
|
|
messageChannel <- message
|
2022-01-06 08:35:44 -06:00
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer cancelFunc()
|
|
|
|
|
go func() {
|
|
|
|
|
err = pubsub.Publish(event, []byte(data))
|
2022-05-24 08:58:39 +01:00
|
|
|
assert.NoError(t, err)
|
2022-01-06 08:35:44 -06:00
|
|
|
}()
|
2022-01-20 10:00:13 -06:00
|
|
|
message := <-messageChannel
|
2022-01-06 08:35:44 -06:00
|
|
|
assert.Equal(t, string(message), data)
|
|
|
|
|
})
|
|
|
|
|
}
|