mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-13 06:20:24 +00:00
When two goroutines attempt to access the content of the buffer log writer, they must be made thread safe with a write mutex. The buffer log writer is only used in testing. ## Checklist ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [ ] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11962 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: limiting-factor <limiting-factor@posteo.com> Co-committed-by: limiting-factor <limiting-factor@posteo.com>
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
// Copyright 2025 The Forgejo Authors.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package log_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"forgejo.org/modules/log"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBufferLogger(t *testing.T) {
|
|
prefix := "TestPrefix "
|
|
level := log.INFO
|
|
expected := "something"
|
|
|
|
bufferWriter := log.NewEventWriterBuffer("test-buffer", log.WriterMode{
|
|
Level: level,
|
|
Prefix: prefix,
|
|
Expression: expected,
|
|
})
|
|
|
|
logger := log.NewLoggerWithWriters(t.Context(), "test", bufferWriter)
|
|
|
|
logger.SendLogEvent(&log.Event{
|
|
Level: log.INFO,
|
|
MsgSimpleText: expected,
|
|
})
|
|
logger.Close()
|
|
assert.Contains(t, bufferWriter.(log.EventWriterBuffer).GetString(), expected)
|
|
}
|
|
|
|
func TestBufferLoggerWithExclusion(t *testing.T) {
|
|
prefix := "ExclusionPrefix "
|
|
level := log.INFO
|
|
message := "something"
|
|
|
|
bufferWriter := log.NewEventWriterBuffer("test-buffer", log.WriterMode{
|
|
Level: level,
|
|
Prefix: prefix,
|
|
Exclusion: message,
|
|
}).(log.EventWriterBuffer)
|
|
|
|
logger := log.NewLoggerWithWriters(t.Context(), "test", bufferWriter)
|
|
|
|
logger.SendLogEvent(&log.Event{
|
|
Level: log.INFO,
|
|
MsgSimpleText: message,
|
|
})
|
|
logger.Close()
|
|
assert.NotContains(t, bufferWriter.GetString(), message)
|
|
}
|
|
|
|
func TestBufferLoggerWithExpressionAndExclusion(t *testing.T) {
|
|
prefix := "BothPrefix "
|
|
level := log.INFO
|
|
expression := ".*foo.*"
|
|
exclusion := ".*bar.*"
|
|
|
|
bufferWriter := log.NewEventWriterBuffer("test-buffer", log.WriterMode{
|
|
Level: level,
|
|
Prefix: prefix,
|
|
Expression: expression,
|
|
Exclusion: exclusion,
|
|
}).(log.EventWriterBuffer)
|
|
|
|
logger := log.NewLoggerWithWriters(t.Context(), "test", bufferWriter)
|
|
|
|
logger.SendLogEvent(&log.Event{Level: log.INFO, MsgSimpleText: "foo expression"})
|
|
logger.SendLogEvent(&log.Event{Level: log.INFO, MsgSimpleText: "bar exclusion"})
|
|
logger.SendLogEvent(&log.Event{Level: log.INFO, MsgSimpleText: "foo bar both"})
|
|
logger.SendLogEvent(&log.Event{Level: log.INFO, MsgSimpleText: "none"})
|
|
logger.Close()
|
|
|
|
assert.Contains(t, bufferWriter.GetString(), "foo expression")
|
|
assert.NotContains(t, bufferWriter.GetString(), "bar")
|
|
}
|