Codebase list golang-github-gosuri-uitable / 8837217
uitable: added concurrency safety Greg Osuri 8 years ago
2 changed file(s) with 26 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
33 import (
44 "fmt"
55 "strings"
6 "sync"
67
78 "github.com/gosuri/uitable/util/strutil"
89 "github.com/gosuri/uitable/util/wordwrap"
2526
2627 // Separator is the seperator for columns in the table. Default is "\t"
2728 Separator string
29
30 mtx *sync.RWMutex
2831 }
2932
3033 // New returns a new Table with default values
3134 func New() *Table {
3235 return &Table{
3336 Separator: Separator,
37 mtx: new(sync.RWMutex),
3438 }
3539 }
3640
3741 // AddRow adds a new row to the table
3842 func (t *Table) AddRow(data ...interface{}) *Table {
43 t.mtx.Lock()
44 defer t.mtx.Unlock()
3945 r := NewRow(data...)
4046 t.Rows = append(t.Rows, r)
4147 return t
4349
4450 // String returns the string value of table
4551 func (t *Table) String() string {
52 t.mtx.RLock()
53 defer t.mtx.RUnlock()
54
4655 if len(t.Rows) == 0 {
4756 return ""
4857 }
00 package uitable
11
22 import (
3 "sync"
34 "testing"
45 )
56
4243 t.Fatalf("need: %q | got: %q ", need, got)
4344 }
4445 }
46
47 func TestAddRow(t *testing.T) {
48 var wg sync.WaitGroup
49 table := New()
50 for i := 0; i < 100; i++ {
51 wg.Add(1)
52 go func() {
53 defer wg.Done()
54 table.AddRow("foo")
55 }()
56 }
57 wg.Wait()
58 if len(table.Rows) != 100 {
59 t.Fatal("want", 100, "got", len(table.Rows))
60 }
61 }