eliminate first update on info stream on cold cache

This commit is contained in:
Umputun
2019-06-04 21:02:28 -05:00
parent 0f2298d193
commit e1bd0e5104
3 changed files with 31 additions and 18 deletions
+7 -5
View File
@@ -168,11 +168,13 @@ func (s *public) infoStreamCtrl(w http.ResponseWriter, r *http.Request) {
if e != nil {
return nil, e
}
if info.LastTS != lastTS || info.Count != lastCount {
lastTS = info.LastTS
lastCount = info.Count // removal won't update lastTS
upd = true // cache update used as indication of post update. comparing lastTS for no-cache
// cache update used as indication of post update. comparing lastTS for no-cache.
// removal won't update lastTS, count check will catch it.
if !lastTS.IsZero() && (info.LastTS != lastTS || info.Count != lastCount) {
upd = true
}
lastTS = info.LastTS
lastCount = info.Count
return encodeJSONWithHTML(info)
})
if err != nil {
@@ -457,7 +459,7 @@ func (s *public) listCtrl(w http.ResponseWriter, r *http.Request) {
}
if err = R.RenderJSONFromBytes(w, r, data); err != nil {
log.Printf("[WARN] can't render posts lits for site %s", siteID)
log.Printf("[WARN] can't render posts list for site %s", siteID)
}
}
+13 -9
View File
@@ -12,6 +12,7 @@ import (
"testing"
"time"
log "github.com/go-pkgz/lgr"
R "github.com/go-pkgz/rest"
"github.com/go-pkgz/rest/cache"
"github.com/stretchr/testify/assert"
@@ -541,7 +542,7 @@ func TestRest_InfoStream(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i < 10; i++ {
for i := 0; i < 10; i++ {
time.Sleep(10 * time.Millisecond)
postComment(t, ts.URL)
}
@@ -553,8 +554,8 @@ func TestRest_InfoStream(t *testing.T) {
recs := strings.Split(strings.TrimSuffix(string(body), "\n"), "\n")
require.Equal(t, 10, len(recs), "10 records")
assert.True(t, strings.Contains(recs[0], `"count":1`), recs[0])
assert.True(t, strings.Contains(recs[9], `"count":10`), recs[9])
assert.True(t, strings.Contains(recs[0], `"count":2`), recs[0])
assert.True(t, strings.Contains(recs[9], `"count":11`), recs[9])
}
func TestRest_InfoStreamTooMany(t *testing.T) {
@@ -601,6 +602,7 @@ func TestRest_InfoStreamTimeout(t *testing.T) {
func TestRest_InfoStreamCancel(t *testing.T) {
ts, srv, teardown := startupT(t)
defer teardown()
srv.pubRest.readOnlyAge = 10000000 // make sure we don't hit read-only
srv.pubRest.streamRefresh = 10 * time.Millisecond
srv.pubRest.streamTimeOut = 500 * time.Millisecond
@@ -608,24 +610,26 @@ func TestRest_InfoStreamCancel(t *testing.T) {
postComment(t, ts.URL)
defer teardown()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i < 10; i++ {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
postComment(t, ts.URL)
log.Printf("write #%d", i)
}
}()
client := http.Client{}
req, err := http.NewRequest("GET", ts.URL+"/api/v1/stream/info?site=radio-t&url=https://radio-t.com/blah1", nil)
require.Nil(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 290*time.Millisecond)
defer cancel()
req = req.WithContext(ctx)
log.Print("start req")
r, err := client.Do(req)
log.Print("end req")
require.Nil(t, err)
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
@@ -635,9 +639,9 @@ func TestRest_InfoStreamCancel(t *testing.T) {
wg.Wait()
recs := strings.Split(strings.TrimSuffix(string(body), "\n"), "\n")
require.Equal(t, 2, len(recs), "2 records")
assert.True(t, strings.Contains(recs[0], `"count":1`), recs[0])
assert.True(t, strings.Contains(recs[1], `"count":2`), recs[1])
require.Equal(t, 2, len(recs), "should have 2 records")
assert.True(t, strings.Contains(recs[0], `"count":2`), recs[0])
assert.True(t, strings.Contains(recs[1], `"count":3`), recs[1])
}
func TestRest_Robots(t *testing.T) {
+11 -4
View File
@@ -39,9 +39,16 @@ import (
var testHTML = "/tmp/test-remark.html"
var getStartedHTML = "/tmp/getstarted.html"
var devToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJyZW1hcms0MiIsImV4cCI6Mzc4OTE5MTgyM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WRtaW4iOnRydWUsImJsb2NrZWQiOmZhbHNlfX19.gAR_sZT7hTx7CNHByyrJQWMB5tAtoiISAiG8kes1IjA"
var adminUmputunToken = "eyJhbGciOiJIUzI1NiJ9." +
"eyJhdWQiOiJyYWRpb3QiLCJleHAiOjE5NTQ1OTc5ODAsImp0aSI6Ijk3YTJlMGFjNGRjN2Q1ZjY5MjZkNWU4NjIwYWNlZjlhNDBjMCIsImlhdCI6MTQ1" +
"NDU5NzY4MCwiaXNzIjoicmVtYXJrNDIiLCJ1c2VyIjp7Im5hbWUiOiJVbXB1dHVuIiwiaWQiOiJnaXRodWJfZWYwZjcwNmE3IiwicGljdHVyZSI6Imh0" +
"dHBzOi8vcmVtYXJrNDIucmFkaW8tdC5jb20vYXBpL3YxL2F2YXRhci9jYjQyZmY0OTNhZGU2OTZkODhhM2E1OTBmMTM2YWU5ZTM0ZGU3YzFiLmltYWdlI" +
"iwiYXR0cnMiOnsiYWRtaW4iOnRydWUsImJsb2NrZWQiOmZhbHNlfX19.gAR_sZT7hTx7CNHByyrJQWMB5tAtoiISAiG8kes1IjA"
func TestRest_FileServer(t *testing.T) {
ts, _, teardown := startupT(t)
@@ -275,8 +282,8 @@ func TestRest_parseError(t *testing.T) {
}
func startupT(t *testing.T) (ts *httptest.Server, srv *Rest, teardown func()) {
log.Setup(log.Debug, log.CallerFile, log.CallerFunc, log.Msec, log.LevelBraces)
// log.Setup(log.Out(ioutil.Discard))
log.Setup(log.CallerFile, log.CallerFunc, log.Msec, log.LevelBraces)
testDb := fmt.Sprintf("/tmp/test-remark-%d.db", rand.Int31())
os.Remove(testDb)