Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions backend/plugins/customize/api/csv_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ func (h *Handlers) ImportIssue(input *plugin.ApiResourceInput) (*plugin.ApiResou
if boardName == "" {
return nil, errors.BadInput.New("empty boardName")
}
err = h.svc.SaveBoard(boardId, boardName)
if err != nil {
return nil, err
}
return nil, h.svc.ImportIssue(boardId, file, incremental)
return nil, h.svc.ImportIssueToBoard(boardId, boardName, file, incremental)
}

// ImportIssueCommit accepts a CSV file, parses and saves it to the database
Expand Down
81 changes: 81 additions & 0 deletions backend/plugins/customize/e2e/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"io"
"os"
"strings"
"testing"

"github.com/apache/devlake/core/models/domainlayer/crossdomain"
"github.com/apache/devlake/core/models/domainlayer/qa"
"github.com/apache/devlake/core/models/domainlayer/ticket"
"github.com/apache/devlake/helpers/e2ehelper"
"github.com/apache/devlake/plugins/customize/impl"
"github.com/apache/devlake/plugins/customize/service"
)

// issueHeader is the header of the issue CSV files that the tests build in memory.
const issueHeader = "id,url,issue_key,title,original_type,original_status,created_date"

// newImportTester returns a DataFlowTester with empty import tables, and a Service on its database.
func newImportTester(t *testing.T) (*e2ehelper.DataFlowTester, *service.Service) {
t.Helper()
var plugin impl.Customize
dataflowTester := e2ehelper.NewDataFlowTester(t, "customize", plugin)
flushImportTables(t, dataflowTester)
return dataflowTester, service.NewService(dataflowTester.Dal)
}

// flushImportTables empties every table that the customize imports read or write.
func flushImportTables(t *testing.T, dataflowTester *e2ehelper.DataFlowTester) {
t.Helper()
dataflowTester.FlushTabler(&ticket.Board{})
dataflowTester.FlushTabler(&ticket.Issue{})
dataflowTester.FlushTabler(&ticket.BoardIssue{})
dataflowTester.FlushTabler(&ticket.IssueLabel{})
dataflowTester.FlushTabler(&ticket.SprintIssue{})
dataflowTester.FlushTabler(&ticket.IssueChangelogs{})
dataflowTester.FlushTabler(&ticket.IssueWorklog{})
dataflowTester.FlushTabler(&ticket.Sprint{})
dataflowTester.FlushTabler(&ticket.BoardSprint{})
dataflowTester.FlushTabler(&crossdomain.Account{})
dataflowTester.FlushTabler(&crossdomain.IssueCommit{})
dataflowTester.FlushTabler(&crossdomain.IssueRepoCommit{})
dataflowTester.FlushTabler(&qa.QaProject{})
dataflowTester.FlushTabler(&qa.QaApi{})
dataflowTester.FlushTabler(&qa.QaTestCase{})
dataflowTester.FlushTabler(&qa.QaTestCaseExecution{})
}

// openCsv opens a CSV file for an import and closes it when the test ends.
func openCsv(t *testing.T, path string) io.ReadCloser {
t.Helper()
file, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = file.Close() })
return file
}

// csvFile returns the lines as the content of a CSV file.
func csvFile(lines ...string) io.ReadCloser {
return io.NopCloser(strings.NewReader(strings.Join(lines, "\n") + "\n"))
}
140 changes: 140 additions & 0 deletions backend/plugins/customize/e2e/import_concurrency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"fmt"
"io"
"strings"
"sync"
"testing"
)

func issueRows(prefix string, n int) []string {
rows := []string{issueHeader}
for i := 1; i <= n; i++ {
rows = append(rows, fmt.Sprintf("%s:%d,https://example.com/%s/%d,%d,title,BUG,new,2024-01-01 00:00:00", prefix, i, prefix, i, i))
}
return rows
}

// An import that is still reading its file must not block writes to the issues and board links
// of other boards. This guards the lock scope of the import transaction, not the original bug:
// it also passes on the code before the fix, which used no transaction.
func TestImportDoesNotLockOtherBoards(t *testing.T) {
dataflowTester, svc := newImportTester(t)
if dataflowTester.Dal.Dialect() != "mysql" {
t.Skip("checks InnoDB locks, and uses innodb_lock_wait_timeout")
}
err := svc.ImportIssue("board-a", csvFile(issueRows("a", 3)...), false)
if err != nil {
t.Fatal(err)
}
err = svc.ImportIssue("board-b", csvFile(issueRows("b", 3)...), false)
if err != nil {
t.Fatal(err)
}

reader, writer := io.Pipe()
// Close the pipe even if the test fails, so that the import releases the import lock.
t.Cleanup(func() { _ = writer.CloseWithError(io.ErrUnexpectedEOF) })
done := make(chan error, 1)
go func() { done <- svc.ImportIssue("board-a", reader, false) }()
// No sleep is needed: Write returns only after the reader has consumed the bytes. The CSV
// reader first reads in NewCsvFileIteratorFromFile, which runs after the deletes, and the
// header plus one row is smaller than its 4096-byte buffer, so one read consumes them.
_, writeErr := writer.Write([]byte(strings.Join(issueRows("a", 1), "\n") + "\n"))
if writeErr != nil {
t.Fatal(writeErr)
}

statements := []string{
"UPDATE issues SET title = 'changed' WHERE id = 'b:1'",
"INSERT INTO board_issues (board_id, issue_id) VALUES ('board-b', 'b:0')",
"INSERT INTO board_issues (board_id, issue_id) VALUES ('board-b', 'b:9')",
"INSERT INTO board_issues (board_id, issue_id) VALUES ('board-a2', 'z:1')",
}
tx := dataflowTester.Db.Begin()
setErr := tx.Exec("SET innodb_lock_wait_timeout = 2").Error
if setErr != nil {
t.Fatal(setErr)
}
var statementErrs []error
for _, statement := range statements {
statementErrs = append(statementErrs, tx.Exec(statement).Error)
}
tx.Rollback()
closeErr := writer.Close()
if closeErr != nil {
t.Fatal(closeErr)
}
if importErr := <-done; importErr != nil {
t.Fatal(importErr)
}
for i, statementErr := range statementErrs {
if statementErr != nil {
t.Errorf("an import of board-a must not lock %q: %v", statements[i], statementErr)
}
}
}

// Two imports that save the same authors in a different order must both succeed.
// This guards the import lock, not the original bug: it also passes on the code before the fix,
// which used no transaction. Without the import lock, the two transactions deadlock on accounts.
func TestConcurrentImportsWithSharedAuthors(t *testing.T) {
_, svc := newImportTester(t)
err := svc.ImportIssue("board-a", csvFile(issueRows("a", 1)...), false)
if err != nil {
t.Fatal(err)
}
err = svc.ImportIssue("board-b", csvFile(issueRows("b", 1)...), false)
if err != nil {
t.Fatal(err)
}
changelogs := func(issueId string, reverse bool) []string {
rows := []string{"id,issue_id,author_name,field_name,original_from_value,original_to_value,created_date"}
for i := 0; i < 200; i++ {
author := i % 40
if reverse {
author = 39 - author
}
rows = append(rows, fmt.Sprintf("%s-%d,%s,author%d,status,Open,Done,2024-01-01 00:00:00", issueId, i, issueId, author))
}
return rows
}
for round := 0; round < 3; round++ {
var wg sync.WaitGroup
errs := make([]error, 2)
for i, issueId := range []string{"a:1", "b:1"} {
wg.Add(1)
go func(i int, issueId string) {
defer wg.Done()
board := "board-" + issueId[:1]
if err := svc.ImportIssueChangelog(board, csvFile(changelogs(issueId, i == 1)...), true); err != nil {
errs[i] = err
}
}(i, issueId)
}
wg.Wait()
for _, err := range errs {
if err != nil {
t.Fatalf("round %d: %v", round, err)
}
}
}
}
Loading
Loading
⌂ Proxy