Compare commits
2
Commits
6773ac95d8
...
bcf14f8985
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcf14f8985 | ||
|
|
d52cac1ec6 |
No files matched your search
@@ -102,6 +102,12 @@ func TestWebhookDBManager_TotalsSurviveReopen(t *testing.T) {
|
|||||||
// seedExpiredEvents stores count events created at the given time,
|
// seedExpiredEvents stores count events created at the given time,
|
||||||
// each with a delivered delivery to one target and a failed delivery
|
// each with a delivered delivery to one target and a failed delivery
|
||||||
// to the other, and one attempt for each delivery.
|
// to the other, and one attempt for each delivery.
|
||||||
|
//
|
||||||
|
// It and seedBareEvents insert 50 rows per statement, not more. The
|
||||||
|
// SQLite driver looks up each parameter's value by scanning all of the
|
||||||
|
// statement's arguments, so the time to bind a statement grows with the
|
||||||
|
// square of its parameter count: at 500 rows, several thousand
|
||||||
|
// parameters, the seeding took most of these tests' time under -race.
|
||||||
func seedExpiredEvents(
|
func seedExpiredEvents(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
db *gorm.DB,
|
db *gorm.DB,
|
||||||
@@ -138,8 +144,8 @@ func seedExpiredEvents(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, db.CreateInBatches(events, 500).Error)
|
require.NoError(t, db.CreateInBatches(events, 50).Error)
|
||||||
require.NoError(t, db.CreateInBatches(deliveries, 500).Error)
|
require.NoError(t, db.CreateInBatches(deliveries, 50).Error)
|
||||||
|
|
||||||
results := make([]database.DeliveryResult, len(deliveries))
|
results := make([]database.DeliveryResult, len(deliveries))
|
||||||
for i := range deliveries {
|
for i := range deliveries {
|
||||||
@@ -148,7 +154,7 @@ func seedExpiredEvents(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, db.CreateInBatches(results, 500).Error)
|
require.NoError(t, db.CreateInBatches(results, 50).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// seedBareEvents stores count events created at the given time, with
|
// seedBareEvents stores count events created at the given time, with
|
||||||
@@ -172,7 +178,7 @@ func seedBareEvents(
|
|||||||
events[i].CreatedAt = createdAt
|
events[i].CreatedAt = createdAt
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, db.CreateInBatches(events, 500).Error)
|
require.NoError(t, db.CreateInBatches(events, 50).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRetentionReaper_PrunesMoreThanOneBatch verifies that a prune
|
// TestRetentionReaper_PrunesMoreThanOneBatch verifies that a prune
|
||||||
|
|||||||
@@ -1911,9 +1911,13 @@ func (h *Handlers) HandleTargetToggle() http.HandlerFunc {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tgt.Active = !tgt.Active
|
// Only the active column: saving the whole row would
|
||||||
|
// write back the name and settings read above over an
|
||||||
|
// edit saved since.
|
||||||
|
active := !tgt.Active
|
||||||
|
|
||||||
return tgt.Active, h.db.DB().Save(&tgt).Error
|
return active, h.db.DB().Model(&tgt).
|
||||||
|
Update("active", active).Error
|
||||||
},
|
},
|
||||||
"failed to toggle target",
|
"failed to toggle target",
|
||||||
targetActivated, targetDeactivated,
|
targetActivated, targetDeactivated,
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package handlers_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestHandleTargetToggle_DoesNotUndoAnEdit proves that a toggle which
|
||||||
|
// loaded the target before an edit of it was saved does not write the
|
||||||
|
// old name and settings back over the edit. The edit is submitted from
|
||||||
|
// a callback on the toggle's own read of the target, so it is saved
|
||||||
|
// after that read and before the toggle writes.
|
||||||
|
func TestHandleTargetToggle_DoesNotUndoAnEdit(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := setupSourceTest(t)
|
||||||
|
wh, tgt := seedHTTPTarget(t, env, "", "")
|
||||||
|
require.True(t, tgt.Active)
|
||||||
|
|
||||||
|
var (
|
||||||
|
edited bool
|
||||||
|
editCode int
|
||||||
|
)
|
||||||
|
|
||||||
|
require.NoError(t, env.db.DB().Callback().Query().
|
||||||
|
After("gorm:query").
|
||||||
|
Register("test:edit_after_toggle_read", func(tx *gorm.DB) {
|
||||||
|
// The edit reads the target too; only the toggle's read,
|
||||||
|
// the first, submits it.
|
||||||
|
if tx.Statement.Table != "targets" || edited {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
edited = true
|
||||||
|
editCode = submitTargetEdit(
|
||||||
|
env, wh.ID, tgt.ID,
|
||||||
|
editForm(editReplacedURL, "", ""),
|
||||||
|
).Code
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
req := postRequest(
|
||||||
|
"/hook/"+wh.ID+"/targets/"+tgt.ID+"/toggle",
|
||||||
|
env.cookies,
|
||||||
|
map[string]string{paramSourceID: wh.ID, paramTargetID: tgt.ID},
|
||||||
|
)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
env.handlers.HandleTargetToggle().ServeHTTP(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusSeeOther, w.Code)
|
||||||
|
require.Equal(t, http.StatusSeeOther, editCode)
|
||||||
|
|
||||||
|
stored := storedTarget(t, env, tgt.ID)
|
||||||
|
assert.False(t, stored.Active)
|
||||||
|
assert.Equal(t, "edited-name", stored.Name)
|
||||||
|
assert.Equal(t, 5, stored.MaxRetries)
|
||||||
|
assert.Equal(
|
||||||
|
t, editReplacedURL, storedHTTPConfig(t, env, tgt.ID).URL,
|
||||||
|
)
|
||||||
|
}
|
||||||
+5
-1
@@ -27,7 +27,11 @@
|
|||||||
# Those figures predate tests hashing the admin password at 1 MB instead of
|
# Those figures predate tests hashing the admin password at 1 MB instead of
|
||||||
# 64 MB (https://git.eeqj.de/sneak/webhooker/pulls/404). After that change, in
|
# 64 MB (https://git.eeqj.de/sneak/webhooker/pulls/404). After that change, in
|
||||||
# a cache-defeated build at host load 44-109 (2026-10-02), internal/handlers
|
# a cache-defeated build at host load 44-109 (2026-10-02), internal/handlers
|
||||||
# took 8.5s and the slowest package was internal/database at 15.8s.
|
# took 8.5s and the slowest package was internal/database at 15.8s. Once its
|
||||||
|
# retention tests seeded 50 rows per insert instead of 500
|
||||||
|
# (https://git.eeqj.de/sneak/webhooker/issues/198), internal/database took
|
||||||
|
# 7.3s and the slowest package was internal/handlers at 8.1s to 10.0s, at host
|
||||||
|
# load 25-48 (2026-10-02).
|
||||||
#
|
#
|
||||||
# -p 4 -parallel 8 keep the run under 2 GB of memory: at most four test
|
# -p 4 -parallel 8 keep the run under 2 GB of memory: at most four test
|
||||||
# binaries build or run at once, each with at most eight parallel tests. Under
|
# binaries build or run at once, each with at most eight parallel tests. Under
|
||||||
|
|||||||
Reference in new issue
Block a user