Fix Poll(); yeyy tests

This commit is contained in:
Philipp Heckel 2021-12-22 23:45:19 +01:00
parent fe5734d9f0
commit 85939618c8
6 changed files with 63 additions and 4 deletions

View file

@ -11,7 +11,7 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies - name: Install dependencies
run: sudo apt update && sudo apt install -y python3-pip run: sudo apt update && sudo apt install -y python3-pip curl
- name: Build docs (required for tests) - name: Build docs (required for tests)
run: make docs run: make docs
- name: Run tests, formatting, vetting and linting - name: Run tests, formatting, vetting and linting

View file

@ -116,6 +116,7 @@ func (c *Client) Poll(topic string, options ...SubscribeOption) ([]*Message, err
msgChan := make(chan *Message) msgChan := make(chan *Message)
errChan := make(chan error) errChan := make(chan error)
topicURL := c.expandTopicURL(topic) topicURL := c.expandTopicURL(topic)
options = append(options, WithPoll())
go func() { go func() {
err := performSubscribeRequest(ctx, msgChan, topicURL, "", options...) err := performSubscribeRequest(ctx, msgChan, topicURL, "", options...)
close(msgChan) close(msgChan)

View file

@ -62,6 +62,36 @@ func TestClient_Publish_Subscribe(t *testing.T) {
require.Nil(t, msg) require.Nil(t, msg)
} }
func TestClient_Publish_Poll(t *testing.T) {
s, port := test.StartServer(t)
defer test.StopServer(t, s, port)
c := client.New(newTestConfig(port))
msg, err := c.Publish("mytopic", "some message", client.WithNoFirebase(), client.WithTagsList("tag1,tag2"))
require.Nil(t, err)
require.Equal(t, "some message", msg.Message)
require.Equal(t, []string{"tag1", "tag2"}, msg.Tags)
msg, err = c.Publish("mytopic", "this won't be cached", client.WithNoCache())
require.Nil(t, err)
require.Equal(t, "this won't be cached", msg.Message)
msg, err = c.Publish("mytopic", "some delayed message", client.WithDelay("20 min"))
require.Nil(t, err)
require.Equal(t, "some delayed message", msg.Message)
messages, err := c.Poll("mytopic")
require.Nil(t, err)
require.Equal(t, 1, len(messages))
require.Equal(t, "some message", messages[0].Message)
messages, err = c.Poll("mytopic", client.WithScheduled())
require.Nil(t, err)
require.Equal(t, 2, len(messages))
require.Equal(t, "some message", messages[0].Message)
require.Equal(t, "some delayed message", messages[1].Message)
}
func newTestConfig(port int) *client.Config { func newTestConfig(port int) *client.Config {
c := client.NewConfig() c := client.NewConfig()
c.DefaultHost = fmt.Sprintf("http://127.0.0.1:%d", port) c.DefaultHost = fmt.Sprintf("http://127.0.0.1:%d", port)

View file

@ -196,16 +196,17 @@ func (s *Server) Run() error {
listenStr += fmt.Sprintf(" %s/https", s.config.ListenHTTPS) listenStr += fmt.Sprintf(" %s/https", s.config.ListenHTTPS)
} }
log.Printf("Listening on %s", listenStr) log.Printf("Listening on %s", listenStr)
http.HandleFunc("/", s.handle) mux := http.NewServeMux()
mux.HandleFunc("/", s.handle)
errChan := make(chan error) errChan := make(chan error)
s.mu.Lock() s.mu.Lock()
s.closeChan = make(chan bool) s.closeChan = make(chan bool)
s.httpServer = &http.Server{Addr: s.config.ListenHTTP} s.httpServer = &http.Server{Addr: s.config.ListenHTTP, Handler: mux}
go func() { go func() {
errChan <- s.httpServer.ListenAndServe() errChan <- s.httpServer.ListenAndServe()
}() }()
if s.config.ListenHTTPS != "" { if s.config.ListenHTTPS != "" {
s.httpsServer = &http.Server{Addr: s.config.ListenHTTP} s.httpsServer = &http.Server{Addr: s.config.ListenHTTP, Handler: mux}
go func() { go func() {
errChan <- s.httpsServer.ListenAndServeTLS(s.config.CertFile, s.config.KeyFile) errChan <- s.httpsServer.ListenAndServeTLS(s.config.CertFile, s.config.KeyFile)
}() }()

View file

@ -487,6 +487,27 @@ func TestServer_SubscribeWithQueryFilters(t *testing.T) {
require.Equal(t, keepaliveEvent, messages[2].Event) require.Equal(t, keepaliveEvent, messages[2].Event)
} }
/*
func TestServer_Curl_Publish_Poll(t *testing.T) {
s, port := test.StartServer(t)
defer test.StopServer(t, s, port)
cmd := exec.Command("sh", "-c", fmt.Sprintf(`curl -sd "This is a test" localhost:%d/mytopic`, port))
require.Nil(t, cmd.Run())
b, err := cmd.CombinedOutput()
require.Nil(t, err)
msg := toMessage(t, string(b))
require.Equal(t, "This is a test", msg.Message)
cmd = exec.Command("sh", "-c", fmt.Sprintf(`curl "localhost:%d/mytopic?poll=1"`, port))
require.Nil(t, cmd.Run())
b, err = cmd.CombinedOutput()
require.Nil(t, err)
msg = toMessage(t, string(b))
require.Equal(t, "This is a test", msg.Message)
}
*/
func newTestConfig(t *testing.T) *Config { func newTestConfig(t *testing.T) *Config {
conf := NewConfig() conf := NewConfig()
conf.CacheFile = filepath.Join(t.TempDir(), "cache.db") conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")

View file

@ -62,6 +62,12 @@ func TestInStringListAll(t *testing.T) {
require.False(t, InStringListAll(s, []string{"three", "five"})) require.False(t, InStringListAll(s, []string{"three", "five"}))
} }
func TestInIntList(t *testing.T) {
s := []int{1, 2}
require.True(t, InIntList(s, 2))
require.False(t, InIntList(s, 3))
}
func TestSplitNoEmpty(t *testing.T) { func TestSplitNoEmpty(t *testing.T) {
require.Equal(t, []string{}, SplitNoEmpty("", ",")) require.Equal(t, []string{}, SplitNoEmpty("", ","))
require.Equal(t, []string{}, SplitNoEmpty(",,,", ",")) require.Equal(t, []string{}, SplitNoEmpty(",,,", ","))