-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wait about a sec before logging message, check presence (#144)
* wait about a sec before logging message, check presence * fix: force flush on **each** 5s of inactivity, instead of first 5sec * bot is useless with arbitrary token and group * don't use common http client, use a custom one with disabled redirects * fixed test, set zero delay for flush tests use pointer receiver for reporter methods to replace http client with mock * fix: in github actions, `os.TempDir` returns path without slash concat path properly * perf: manually stop ticker * doc: updated readme * added repeater, comments, some cosmetic changes * doc: moved comments to the corresponding places * revert imports sort by @Semior001
- Loading branch information
Showing
5 changed files
with
252 additions
and
48 deletions.
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,92 @@ | ||
package reporter | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"github.com/radio-t/super-bot/app/bot" | ||
"os" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/radio-t/super-bot/app/bot" | ||
"strconv" | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"io" | ||
"bytes" | ||
"path" | ||
) | ||
|
||
var logs = "logs" | ||
var msg = bot.Message{Text: "1st"} | ||
var msg = bot.Message{ID: 101, Text: "1st"} | ||
|
||
func TestNewLogger(t *testing.T) { | ||
defer os.RemoveAll(logs) | ||
reporter := NewLogger(logs) | ||
assert.NotNil(t, reporter) | ||
assert.DirExists(t, logs) | ||
|
||
tbl := []struct { | ||
count int | ||
timeout time.Duration | ||
}{ | ||
{101, 100 * time.Millisecond}, | ||
{1, 6 * time.Second}, | ||
} | ||
|
||
for i, tt := range tbl { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
for i = 0; i < tt.count; i++ { | ||
reporter.Save(&msg) | ||
} | ||
time.Sleep(tt.timeout) | ||
logfile := fmt.Sprintf("%s/%s.log", logs, time.Now().Format("20060102")) | ||
assert.FileExists(t, logfile) | ||
err := os.Remove(logfile) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
t.Run("logger saves messages", func(t *testing.T) { | ||
tbl := []struct { | ||
count int | ||
timeout time.Duration | ||
}{ | ||
{101, 100 * time.Millisecond}, | ||
{1, 6 * time.Second}, | ||
} | ||
|
||
for i, tt := range tbl { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
p := path.Join(os.TempDir(), "superbot_logs", strconv.Itoa(i)) | ||
defer os.RemoveAll(p) | ||
|
||
clientMock := &httpClientMock{ | ||
GetFunc: func(url string) (*http.Response, error) { | ||
assert.Equal(t, "https://t.me/radio_t_chat/101?single", url) | ||
return &http.Response{ | ||
StatusCode: 302, | ||
Body: io.NopCloser(bytes.NewBuffer([]byte(""))), | ||
}, nil | ||
}, | ||
} | ||
|
||
reporter := NewLogger(p, 0, "radio_t_chat") | ||
reporter.httpCl = clientMock | ||
assert.NotNil(t, reporter) | ||
assert.DirExists(t, p) | ||
|
||
for i = 0; i < tt.count; i++ { | ||
reporter.Save(&msg) | ||
} | ||
time.Sleep(tt.timeout) | ||
logfile := fmt.Sprintf("%s/%s.log", p, time.Now().Format("20060102")) | ||
assert.FileExists(t, logfile) | ||
require.NoError(t, os.Remove(logfile)) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("logger skips deleted messages", func(t *testing.T) { | ||
path, err := os.MkdirTemp("", "superbot_logs") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(path) | ||
|
||
var startedAt time.Time | ||
clientMock := &httpClientMock{ | ||
GetFunc: func(url string) (*http.Response, error) { | ||
actualDelay := time.Since(startedAt) | ||
assert.Equal(t, "https://t.me/radio_t_chat/101?single", url) | ||
assert.True(t, actualDelay > 500*time.Millisecond && actualDelay < 600*time.Millisecond, | ||
"delay expected 500ms, got %v", actualDelay) | ||
return &http.Response{ | ||
StatusCode: 302, | ||
Body: io.NopCloser(bytes.NewBuffer([]byte(""))), | ||
}, nil | ||
}, | ||
} | ||
reporter := NewLogger(path, 500*time.Millisecond, "radio_t_chat") | ||
reporter.httpCl = clientMock | ||
assert.NotNil(t, reporter) | ||
assert.DirExists(t, path) | ||
|
||
startedAt = time.Now() | ||
reporter.Save(&bot.Message{ID: 101, Text: "something"}) | ||
|
||
time.Sleep(6 * time.Second) // wait for forced flush | ||
logfile := fmt.Sprintf("%s/%s.log", path, time.Now().Format("20060102")) | ||
assert.FileExists(t, logfile) | ||
require.NoError(t, os.Remove(logfile)) | ||
}) | ||
} |