Skip to content

Commit

Permalink
Fix racing condition of swipe management
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 23, 2024
1 parent b6f9dbf commit c174404
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fmt: ## Format code
printf "${GREEN} Code formatted.\n\n"; \

.PHONY: test-ci
test: ## Run tests for CI
test-ci: ## Run tests for CI
printf "${GREEN}Run all tests\n\n${WHITE}"; \
go clean -cache -testcache -i -r; \
go test -cover -race -json -v ./... ; \
Expand Down
2 changes: 1 addition & 1 deletion backend/graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

// NOTE : This test does not use testify.suite because
// Testify suite does not allow to run tests parallel, which is considered inevitable for resolver tests.
// Testify suite does not allow to run tests Parallel, which is considered inevitable for resolver tests.
var e *echo.Echo
var db *gorm.DB
var migrationFilePath = "../db/migrations"
Expand Down
27 changes: 18 additions & 9 deletions backend/pkg/textdic/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions backend/pkg/textdic/parser.y
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%{
package textdic

import "sync"

// Define Node and Nodes types
type Node struct {
Word string
Expand All @@ -12,6 +14,7 @@ type Nodes []Node
%}

%union {
mutex sync.RWMutex
str string
node Node
nodes Nodes
Expand Down Expand Up @@ -49,14 +52,20 @@ type Parser interface {

func NewParser(yylex yyLexer) Parser {
yyparser := &yyParserImpl{}
yyparser.lval.mutex.RLock()
defer yyparser.lval.mutex.RUnlock()
yyparser.Parse(yylex)
return yyparser
}

func (yyrcvr *yyParserImpl) setNodes(nodes []Node) {
yyrcvr.lval.mutex.RLock()
defer yyrcvr.lval.mutex.RUnlock()
yyrcvr.lval.nodes = nodes
}

func (yyrcvr *yyParserImpl) GetNodes() []Node {
yyrcvr.lval.mutex.RLock()
defer yyrcvr.lval.mutex.RUnlock()
return yyrcvr.lval.nodes
}
18 changes: 13 additions & 5 deletions backend/pkg/textdic/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package textdic

import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

var mutext sync.RWMutex

func TestParser(t *testing.T) {
t.Parallel()

// Run TestParserService
t.Run("TestSmoke", func(t *testing.T) {
t.Parallel()

// Test input
var input = `
Expand Down Expand Up @@ -58,7 +62,8 @@ There is no leeway to provide services free of charge for the sake of others.
}

for _, tc := range testCases {

mutext.RLock()
defer mutext.RUnlock()
// Create a new lexer with the input
l := newLexer(tc.input)

Expand All @@ -81,7 +86,8 @@ There is no leeway to provide services free of charge for the sake of others.
// New test case to check for errors
t.Run("TestErrors", func(t *testing.T) {
t.Parallel()

mutext.RLock()
defer mutext.RUnlock()
// Test input that will cause a parsing error
var input = `
trot out 自慢げに話題に持ち出す
Expand All @@ -90,8 +96,10 @@ trot out 自慢げに話題に持ち出す
`
// Create a new lexer with the input
l := newLexer(input)
yyDebug = 5
yyErrorVerbose = true

// For Debug
//yyDebug = 5
//yyErrorVerbose = true

// Parse the input using the parser instance
parser := NewParser(l)
Expand Down
11 changes: 9 additions & 2 deletions backend/pkg/textdic/text_dictionary_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package textdic
import (
"encoding/base64"
"fmt"
"sync"
)

// textDictionaryService struct definition
type textDictionaryService struct{}
type textDictionaryService struct {
mu sync.RWMutex
}

// TextDictionaryService defines the methods for processing text dictionaries.
type TextDictionaryService interface {
Expand All @@ -21,11 +24,15 @@ func NewTextDictionaryService() TextDictionaryService {

// Process processes a given dictionary string and returns the parsed Nodes or an error
func (tds *textDictionaryService) Process(dic string) ([]Node, []error) {
tds.mu.RLock()
defer tds.mu.RUnlock()

// Use the new parser to parse the input
l := newLexer(dic)

// Parse the input using the new parser
yyErrorVerbose = true
//yyErrorVerbose = true

parser := NewParser(l)
parsedNodes := parser.GetNodes()

Expand Down
16 changes: 14 additions & 2 deletions backend/pkg/textdic/text_dictionary_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import (
"testing"
)

var mu sync.RWMutex

func TestParserService(t *testing.T) {
t.Helper()
t.Parallel()

// Test input
var input = `
trot out 自慢げに話題に持ち出す
Expand Down Expand Up @@ -80,12 +85,16 @@ Hold me accountable for 自分の行動の結果を受け入れ、罰を受け
tc := tc // capture range variable to avoid issues in parallel tests
t.Run(tc.name, func(t *testing.T) {
t.Parallel() // Mark the test to run in parallel
mu.RLock()
defer mu.RUnlock()

// Create a new parser service
service := NewTextDictionaryService()

yyDebug = 5
yyErrorVerbose = true
// For Debug
//yyDebug = 5
//yyErrorVerbose = true

// Process the dictionary input
parsedNodes, err := service.Process(tc.input)
if err != nil {
Expand All @@ -108,6 +117,7 @@ Hold me accountable for 自分の行動の結果を受け入れ、罰を受け

// Run TestParserService_ErrorCases
t.Run("TestParserService_ErrorCases", func(t *testing.T) {
t.Parallel() // Mark the test to run in parallel
// Define error test cases
errorTestCases := []struct {
name string
Expand All @@ -133,6 +143,8 @@ Hold me accountable for 自分の行動の結果を受け入れ、罰を受け
tc := tc // capture range variable to avoid issues in parallel tests
t.Run(tc.name, func(t *testing.T) {
t.Parallel() // Mark the test to run in parallel
mu.RLock()
defer mu.RUnlock()

// Create a new parser service
service := NewTextDictionaryService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
repo "backend/pkg/repository"
"backend/testutils"
"context"
"github.com/labstack/echo/v4"
"log"
"math/rand"
"strconv"
Expand All @@ -21,7 +20,6 @@ import (
)

var db *gorm.DB
var e *echo.Echo
var sv services.Services
var userService services.UserService
var cardGroupService services.CardGroupService
Expand Down

0 comments on commit c174404

Please sign in to comment.