Ясен обнови решението на 16.11.2014 19:18 (преди над 3 години)
Грешка в Set-a, разменям 53-54 ред.
PASS ok _/tmp/d20141204-6466-fus44x 0.015s PASS ok _/tmp/d20141204-6466-fus44x 0.017s panic: test timed out goroutine 15 [running]: testing.alarm() /usr/local/lib/go/src/pkg/testing/testing.go:533 +0x44 created by time.goFunc /usr/local/lib/go/src/pkg/time/sleep.go:122 +0x45 goroutine 1 [chan receive]: testing.RunTests(0x8148c90, 0x81c2ba0, 0x19, 0x19, 0x1, ...) /usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f testing.Main(0x8148c90, 0x81c2ba0, 0x19, 0x19, 0x81c5600, ...) /usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69 main.main() _/tmp/d20141204-6466-fus44x/_test/_testmain.go:91 +0x81 goroutine 4 [sleep]: time.Sleep(0x3c336080, 0x0) /usr/local/lib/go/src/pkg/runtime/ztime_linux_386.c:19 +0x3a _/tmp/d20141204-6466-fus44x.TestSizes(0x183611e0) /tmp/d20141204-6466-fus44x/solution_test.go:111 +0x3d9 testing.tRunner(0x183611e0, 0x81c2bb8) /usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87 created by testing.RunTests /usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684 goroutine 5 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300268, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 6 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300290, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 7 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x183002b8, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 8 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x183002e0, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 9 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300308, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 10 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300330, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 11 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300358, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 12 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x18300380, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 13 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x183003a8, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 14 [runnable]: _/tmp/d20141204-6466-fus44x.func·001(0x1834e0a0, 0x183003d0, 0x5) /tmp/d20141204-6466-fus44x/solution.go:64 +0x19b created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 exit status 2 FAIL _/tmp/d20141204-6466-fus44x 1.013s PASS ok _/tmp/d20141204-6466-fus44x 0.122s PASS ok _/tmp/d20141204-6466-fus44x 0.175s PASS ok _/tmp/d20141204-6466-fus44x 0.016s PASS ok _/tmp/d20141204-6466-fus44x 0.012s --- FAIL: TestSimpleIncrementAndDecrementCalls-2 (0.00 seconds) solution_test.go:288: Decrementing non-number value did not return error FAIL exit status 1 FAIL _/tmp/d20141204-6466-fus44x 0.012s PASS ok _/tmp/d20141204-6466-fus44x 0.012s --- FAIL: TestIncAndDecInManyRoutines-2 (0.05 seconds) solution_test.go:388: Expected 666 but found 2468 FAIL exit status 1 FAIL _/tmp/d20141204-6466-fus44x 0.060s PASS ok _/tmp/d20141204-6466-fus44x 0.012s PASS ok _/tmp/d20141204-6466-fus44x 0.012s PASS ok _/tmp/d20141204-6466-fus44x 0.012s PASS ok _/tmp/d20141204-6466-fus44x 0.012s PASS ok _/tmp/d20141204-6466-fus44x 0.112s PASS ok _/tmp/d20141204-6466-fus44x 0.062s --- FAIL: TestExpiredChanWhenNoOneIsReading-2 (0.06 seconds) solution_test.go:554: Wrong key expired FAIL exit status 1 FAIL _/tmp/d20141204-6466-fus44x 0.072s PASS ok _/tmp/d20141204-6466-fus44x 0.112s PASS ok _/tmp/d20141204-6466-fus44x 0.132s PASS ok _/tmp/d20141204-6466-fus44x 0.259s PASS ok _/tmp/d20141204-6466-fus44x 0.012s --- FAIL: TestDestroyMethodClosesExpireChannel-2 (0.05 seconds) solution_test.go:818: Expire channel was not closed in time FAIL exit status 1 FAIL _/tmp/d20141204-6466-fus44x 0.062s PASS ok _/tmp/d20141204-6466-fus44x 0.012s panic: test timed out goroutine 20 [running]: testing.alarm() /usr/local/lib/go/src/pkg/testing/testing.go:533 +0x44 created by time.goFunc /usr/local/lib/go/src/pkg/time/sleep.go:122 +0x45 goroutine 1 [chan receive]: testing.RunTests(0x8148c90, 0x81c2ba0, 0x19, 0x19, 0x1, ...) /usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f testing.Main(0x8148c90, 0x81c2ba0, 0x19, 0x19, 0x81c5600, ...) /usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69 main.main() _/tmp/d20141204-6466-fus44x/_test/_testmain.go:91 +0x81 goroutine 4 [semacquire]: sync.runtime_Semacquire(0x18300268) /usr/local/lib/go/src/pkg/runtime/zsema_linux_386.c:165 +0x32 sync.(*WaitGroup).Wait(0x1834ea2c) /usr/local/lib/go/src/pkg/sync/waitgroup.go:109 +0xda _/tmp/d20141204-6466-fus44x.(*ExpireMap).Destroy(0x1834ea00) /tmp/d20141204-6466-fus44x/solution.go:260 +0xa8 _/tmp/d20141204-6466-fus44x.TestMultipleKeysWithTheSameExpireTime(0x1837c120) /tmp/d20141204-6466-fus44x/solution_test.go:892 +0x74e testing.tRunner(0x1837c120, 0x81c2cb4) /usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87 created by testing.RunTests /usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684 goroutine 5 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351200, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 6 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351210, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 7 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351230, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 8 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351240, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 9 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351250, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 10 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351260, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 11 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351270, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 13 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18351290, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 14 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x183512a0, 0xf) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 15 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18331840, 0x10) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 16 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18331860, 0x10) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 17 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x18331880, 0x10) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 18 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x183318a0, 0x10) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 goroutine 19 [chan send]: _/tmp/d20141204-6466-fus44x.func·001(0x1834ea00, 0x183318c0, 0x10) /tmp/d20141204-6466-fus44x/solution.go:74 +0x16e created by _/tmp/d20141204-6466-fus44x.(*ExpireMap).Set /tmp/d20141204-6466-fus44x/solution.go:80 +0x166 exit status 2 FAIL _/tmp/d20141204-6466-fus44x 1.013s PASS ok _/tmp/d20141204-6466-fus44x 0.012s
Грешка в Set-a, разменям 53-54 ред.
хм .... Не бих казал че ми харесва, но изглежда сравнително работещо ОСВЕН ... нека предположим че никой не чете от ExpiredChan ? какво ще се случи ? А ако никой не чете от него и ти викнеш Destroy след като някой ключ е expired-нал ? подсказка - втория sleep е за да не излезе преди горотината да се доопита да пише във вече затворения канал :).
Можеш да намалиш броя на map-овете които ползваш :)
в допълнение има type switch
Ех... жалко, че не ти харесва! :P
Ако предположим, че никой не чете от ExpiredChan, горутините си висят зациклили в опит да пишат в този чан (ExpireMap.echan
). Както се разбрахме на лекциите, горутините не са много скъпи, така че нямам проблем с това да си висят до края на програмата.
ОБАЧЕ, както ми насочи вниманието:
Проверката за това дали е унищожен ExpireMap-a върши работа само в случая, че някой е решил да Expire-ва след спирането на цялата работа. Не върши работа за цялата опашка от хора, които вече чакат да пишат.
Това ще го оправям като предефинирам буфериран канал, който да поеме всичките им заявки, точно преди да затворя всичко.
;)
Type switch ще го прилагам на място, също.
За да не използвам два мапа, ще си направя структ, който да съдържа:
interface{}
time.Duration expiration
И тъй!
Благодаря за фийдбека! :)
Реших да не използвам type switch, защото поради някаква причина смята, че променливата, по която switch-ва, не се използва (и не компилира кодът).
А аз всъщност бих я използвал тази променлива само за switch-ване.
Например:
switch p := мистерия.(type) {
default:
fmt.Println("This is no type of mine!")
case int:
fmt.Printf("This %v is one true int!\n", мистерия)
}
Хвърля, че p не се използва, а се декларира напразно! Ето тук е нагледно.
Ще направя да се заключва когато и чете каквото и да е.
Ама не, всъщност не е много добре да си висят току така!
Я да им видя сметката накрая...