From dfbef878aaf4e9e191d83eb9dbdc2a3992aa6ecb Mon Sep 17 00:00:00 2001 From: gerald1248 Date: Tue, 13 Dec 2016 22:22:19 +0100 Subject: [PATCH 1/4] added test for concurrent text drawing --- sync_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 sync_test.go diff --git a/sync_test.go b/sync_test.go new file mode 100644 index 0000000..addc028 --- /dev/null +++ b/sync_test.go @@ -0,0 +1,97 @@ +// See also test_test.go + +package draw2d_test + +import ( + "fmt" + "github.com/golang/freetype/truetype" + "github.com/llgcode/draw2d" + "github.com/llgcode/draw2d/draw2dimg" + "github.com/llgcode/draw2d/draw2dkit" + "image" + "io/ioutil" + "path/filepath" + "sync" + "testing" +) + +func TestSync(t *testing.T) { + ch := make(chan int) + limit := 200 + for i := 0; i < limit; i++ { + go Draw(i, ch) + } + + for i := 0; i < limit; i++ { + counter := <-ch + t.Logf("Goroutine %d returned\n", counter) + } +} + +func Draw(i int, ch chan<- int) { + draw2d.SetFontCache(testCache) + + // Draw a rounded rectangle using default colors + dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) + gc := draw2dimg.NewGraphicContext(dest) + + draw2dkit.RoundedRectangle(gc, 5, 5, 135, 95, 10, 10) + gc.FillStroke() + + // Set the fill text color to black + gc.SetFillColor(image.Black) + gc.SetFontSize(14) + + // Display Hello World dimensions + x1, y1, x2, y2 := gc.GetStringBounds("Hello world") + gc.FillStringAt(fmt.Sprintf("%.2f %.2f %.2f %.2f", x1, y1, x2, y2), 0, 0) + + ch <- i +} + +//testFontCache closely follows draw2d's defaultFontCache +type testFontCache struct { + fonts map[string]*truetype.Font + folder string + namer draw2d.FontFileNamer +} + +func (cache *testFontCache) Load(fontData draw2d.FontData) (font *truetype.Font, err error) { + if font = cache.fonts[cache.namer(fontData)]; font != nil { + return font, nil + } + + var data []byte + var file = cache.namer(fontData) + + if data, err = ioutil.ReadFile(filepath.Join(cache.folder, file)); err != nil { + return + } + + if font, err = truetype.Parse(data); err != nil { + return + } + + var mu sync.Mutex + mu.Lock() + cache.fonts[file] = font + mu.Unlock() + return +} + +func (cache *testFontCache) Store(fontData draw2d.FontData, font *truetype.Font) { + var mu sync.Mutex + mu.Lock() + cache.fonts[cache.namer(fontData)] = font + mu.Unlock() +} + +var ( + testFonts = &testFontCache{ + fonts: make(map[string]*truetype.Font), + folder: "./resource/font", + namer: draw2d.FontFileName, + } + + testCache draw2d.FontCache = testFonts +) From eca7b76ebce92efaaabd5a11f915dab3645ed2f2 Mon Sep 17 00:00:00 2001 From: gerald1248 Date: Tue, 13 Dec 2016 22:23:50 +0100 Subject: [PATCH 2/4] fmt --- sync_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync_test.go b/sync_test.go index addc028..9a3ae07 100644 --- a/sync_test.go +++ b/sync_test.go @@ -28,7 +28,7 @@ func TestSync(t *testing.T) { } } -func Draw(i int, ch chan<- int) { +func Draw(i int, ch chan<- int) { draw2d.SetFontCache(testCache) // Draw a rounded rectangle using default colors From 7c57ea38bb0386820d5373d553cff7cf9e3cbd2b Mon Sep 17 00:00:00 2001 From: gerald1248 Date: Fri, 6 Jan 2017 22:22:34 +0100 Subject: [PATCH 3/4] removed all trickery, left only plain draw2d calls --- sync_test.go | 58 +++------------------------------------------------- 1 file changed, 3 insertions(+), 55 deletions(-) diff --git a/sync_test.go b/sync_test.go index 9a3ae07..156851a 100644 --- a/sync_test.go +++ b/sync_test.go @@ -1,23 +1,19 @@ -// See also test_test.go +// go test --race -test.v sync_test.go package draw2d_test import ( "fmt" - "github.com/golang/freetype/truetype" "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dimg" "github.com/llgcode/draw2d/draw2dkit" "image" - "io/ioutil" - "path/filepath" - "sync" "testing" ) func TestSync(t *testing.T) { ch := make(chan int) - limit := 200 + limit := 2000 for i := 0; i < limit; i++ { go Draw(i, ch) } @@ -29,8 +25,7 @@ func TestSync(t *testing.T) { } func Draw(i int, ch chan<- int) { - draw2d.SetFontCache(testCache) - + draw2d.SetFontFolder("./resource/font") // Draw a rounded rectangle using default colors dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) gc := draw2dimg.NewGraphicContext(dest) @@ -48,50 +43,3 @@ func Draw(i int, ch chan<- int) { ch <- i } - -//testFontCache closely follows draw2d's defaultFontCache -type testFontCache struct { - fonts map[string]*truetype.Font - folder string - namer draw2d.FontFileNamer -} - -func (cache *testFontCache) Load(fontData draw2d.FontData) (font *truetype.Font, err error) { - if font = cache.fonts[cache.namer(fontData)]; font != nil { - return font, nil - } - - var data []byte - var file = cache.namer(fontData) - - if data, err = ioutil.ReadFile(filepath.Join(cache.folder, file)); err != nil { - return - } - - if font, err = truetype.Parse(data); err != nil { - return - } - - var mu sync.Mutex - mu.Lock() - cache.fonts[file] = font - mu.Unlock() - return -} - -func (cache *testFontCache) Store(fontData draw2d.FontData, font *truetype.Font) { - var mu sync.Mutex - mu.Lock() - cache.fonts[cache.namer(fontData)] = font - mu.Unlock() -} - -var ( - testFonts = &testFontCache{ - fonts: make(map[string]*truetype.Font), - folder: "./resource/font", - namer: draw2d.FontFileName, - } - - testCache draw2d.FontCache = testFonts -) From 0cf6b8d61f2f2c443cb134bcb4d85578ca16105e Mon Sep 17 00:00:00 2001 From: gerald1248 Date: Fri, 6 Jan 2017 22:24:37 +0100 Subject: [PATCH 4/4] fixed typo --- sync_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync_test.go b/sync_test.go index 156851a..76d5aed 100644 --- a/sync_test.go +++ b/sync_test.go @@ -1,4 +1,4 @@ -// go test --race -test.v sync_test.go +// go test -race -test.v sync_test.go package draw2d_test