Skip to content

Commit

Permalink
Fixes deadlock in the ToSlice method. (#39)
Browse files Browse the repository at this point in the history
* Fixes deadlock in the ToSlice method.

* Updating travis to be something more recent.
  • Loading branch information
deckarep authored Dec 13, 2016
1 parent ceca003 commit 58ba19e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: go

go:
- 1.2
- 1.3
- 1.5
- 1.6
- 1.7
- tip

script:
Expand Down
2 changes: 1 addition & 1 deletion threadsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ func (set *threadSafeSet) CartesianProduct(other Set) Set {
}

func (set *threadSafeSet) ToSlice() []interface{} {
set.RLock()
keys := make([]interface{}, 0, set.Cardinality())
set.RLock()
for elem := range set.s {
keys = append(keys, elem)
}
Expand Down
21 changes: 21 additions & 0 deletions threadsafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,27 @@ func Test_ToSlice(t *testing.T) {
}
}

// Test_ToSliceDeadlock - fixes issue: https://github.com/deckarep/golang-set/issues/36
// This code reveals the deadlock however it doesn't happen consistently.
func Test_ToSliceDeadlock(t *testing.T) {
runtime.GOMAXPROCS(2)

var wg sync.WaitGroup
set := NewSet()
workers := 10
wg.Add(workers)
for i := 1; i <= workers; i++ {
go func() {
for j := 0; j < 1000; j++ {
set.Add(1)
set.ToSlice()
}
wg.Done()
}()
}
wg.Wait()
}

func Test_UnmarshalJSON(t *testing.T) {
s := []byte(`["test", 1, 2, 3, ["4,5,6"]]`)
expected := NewSetFromSlice(
Expand Down

0 comments on commit 58ba19e

Please sign in to comment.