How to use CheckPanic method of test Package

Best Go-testdeep code snippet using test.CheckPanic

feed_test.go

Source:feed_test.go Github

copy

Full Screen

1// Copyright 2016 The go-ethereum Authors2// This file is part of the go-ethereum library.3//4// The go-ethereum library is free software: you can redistribute it and/or modify5// it under the terms of the GNU Lesser General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.8//9// The go-ethereum library is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU Lesser General Public License for more details.13//14// You should have received a copy of the GNU Lesser General Public License15// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.16package event17import (18 "fmt"19 "reflect"20 "sync"21 "testing"22 "time"23)24func TestFeedPanics(t *testing.T) {25 {26 var f Feed27 f.Send(int(2))28 want := feedTypeError{op: "Send", got: reflect.TypeOf(uint64(0)), want: reflect.TypeOf(int(0))}29 if err := checkPanic(want, func() { f.Send(uint64(2)) }); err != nil {30 t.Error(err)31 }32 }33 {34 var f Feed35 ch := make(chan int)36 f.Subscribe(ch)37 want := feedTypeError{op: "Send", got: reflect.TypeOf(uint64(0)), want: reflect.TypeOf(int(0))}38 if err := checkPanic(want, func() { f.Send(uint64(2)) }); err != nil {39 t.Error(err)40 }41 }42 {43 var f Feed44 f.Send(int(2))45 want := feedTypeError{op: "Subscribe", got: reflect.TypeOf(make(chan uint64)), want: reflect.TypeOf(make(chan<- int))}46 if err := checkPanic(want, func() { f.Subscribe(make(chan uint64)) }); err != nil {47 t.Error(err)48 }49 }50 {51 var f Feed52 if err := checkPanic(errBadChannel, func() { f.Subscribe(make(<-chan int)) }); err != nil {53 t.Error(err)54 }55 }56 {57 var f Feed58 if err := checkPanic(errBadChannel, func() { f.Subscribe(int(0)) }); err != nil {59 t.Error(err)60 }61 }62}63func checkPanic(want error, fn func()) (err error) {64 defer func() {65 panic := recover()66 if panic == nil {67 err = fmt.Errorf("didn't panic")68 } else if !reflect.DeepEqual(panic, want) {69 err = fmt.Errorf("panicked with wrong error: got %q, want %q", panic, want)70 }71 }()72 fn()73 return nil74}75func TestFeed(t *testing.T) {76 var feed Feed77 var done, subscribed sync.WaitGroup78 subscriber := func(i int) {79 defer done.Done()80 subchan := make(chan int)81 sub := feed.Subscribe(subchan)82 timeout := time.NewTimer(2 * time.Second)83 subscribed.Done()84 select {85 case v := <-subchan:86 if v != 1 {87 t.Errorf("%d: received value %d, want 1", i, v)88 }89 case <-timeout.C:90 t.Errorf("%d: receive timeout", i)91 }92 sub.Unsubscribe()93 select {94 case _, ok := <-sub.Err():95 if ok {96 t.Errorf("%d: error channel not closed after unsubscribe", i)97 }98 case <-timeout.C:99 t.Errorf("%d: unsubscribe timeout", i)100 }101 }102 const n = 1000103 done.Add(n)104 subscribed.Add(n)105 for i := 0; i < n; i++ {106 go subscriber(i)107 }108 subscribed.Wait()109 if nsent := feed.Send(1); nsent != n {110 t.Errorf("first send delivered %d times, want %d", nsent, n)111 }112 if nsent := feed.Send(2); nsent != 0 {113 t.Errorf("second send delivered %d times, want 0", nsent)114 }115 done.Wait()116}117func TestFeedSubscribeSameChannel(t *testing.T) {118 var (119 feed Feed120 done sync.WaitGroup121 ch = make(chan int)122 sub1 = feed.Subscribe(ch)123 sub2 = feed.Subscribe(ch)124 _ = feed.Subscribe(ch)125 )126 expectSends := func(value, n int) {127 if nsent := feed.Send(value); nsent != n {128 t.Errorf("send delivered %d times, want %d", nsent, n)129 }130 done.Done()131 }132 expectRecv := func(wantValue, n int) {133 for i := 0; i < n; i++ {134 if v := <-ch; v != wantValue {135 t.Errorf("received %d, want %d", v, wantValue)136 }137 }138 }139 done.Add(1)140 go expectSends(1, 3)141 expectRecv(1, 3)142 done.Wait()143 sub1.Unsubscribe()144 done.Add(1)145 go expectSends(2, 2)146 expectRecv(2, 2)147 done.Wait()148 sub2.Unsubscribe()149 done.Add(1)150 go expectSends(3, 1)151 expectRecv(3, 1)152 done.Wait()153}154func TestFeedSubscribeBlockedPost(t *testing.T) {155 var (156 feed Feed157 nsends = 2000158 ch1 = make(chan int)159 ch2 = make(chan int)160 wg sync.WaitGroup161 )162 defer wg.Wait()163 feed.Subscribe(ch1)164 wg.Add(nsends)165 for i := 0; i < nsends; i++ {166 go func() {167 feed.Send(99)168 wg.Done()169 }()170 }171 sub2 := feed.Subscribe(ch2)172 defer sub2.Unsubscribe()173 // We're done when ch1 has received N times.174 // The number of receives on ch2 depends on scheduling.175 for i := 0; i < nsends; {176 select {177 case <-ch1:178 i++179 case <-ch2:180 }181 }182}183func TestFeedUnsubscribeBlockedPost(t *testing.T) {184 var (185 feed Feed186 nsends = 200187 chans = make([]chan int, 2000)188 subs = make([]Subscription, len(chans))189 bchan = make(chan int)190 bsub = feed.Subscribe(bchan)191 wg sync.WaitGroup192 )193 for i := range chans {194 chans[i] = make(chan int, nsends)195 }196 // Queue up some Sends. None of these can make progress while bchan isn't read.197 wg.Add(nsends)198 for i := 0; i < nsends; i++ {199 go func() {200 feed.Send(99)201 wg.Done()202 }()203 }204 // Subscribe the other channels.205 for i, ch := range chans {206 subs[i] = feed.Subscribe(ch)207 }208 // Unsubscribe them again.209 for _, sub := range subs {210 sub.Unsubscribe()211 }212 // Unblock the Sends.213 bsub.Unsubscribe()214 wg.Wait()215}216func TestFeedUnsubscribeFromInbox(t *testing.T) {217 var (218 feed Feed219 ch1 = make(chan int)220 ch2 = make(chan int)221 sub1 = feed.Subscribe(ch1)222 sub2 = feed.Subscribe(ch1)223 sub3 = feed.Subscribe(ch2)224 )225 if len(feed.inbox) != 3 {226 t.Errorf("inbox length != 3 after subscribe")227 }228 if len(feed.sendCases) != 1 {229 t.Errorf("sendCases is non-empty after unsubscribe")230 }231 sub1.Unsubscribe()232 sub2.Unsubscribe()233 sub3.Unsubscribe()234 if len(feed.inbox) != 0 {235 t.Errorf("inbox is non-empty after unsubscribe")236 }237 if len(feed.sendCases) != 1 {238 t.Errorf("sendCases is non-empty after unsubscribe")239 }240}241func BenchmarkFeedSend1000(b *testing.B) {242 var (243 done sync.WaitGroup244 feed Feed245 nsubs = 1000246 )247 subscriber := func(ch <-chan int) {248 for i := 0; i < b.N; i++ {249 <-ch250 }251 done.Done()252 }253 done.Add(nsubs)254 for i := 0; i < nsubs; i++ {255 ch := make(chan int, 200)256 feed.Subscribe(ch)257 go subscriber(ch)258 }259 // The actual benchmark.260 b.ResetTimer()261 for i := 0; i < b.N; i++ {262 if feed.Send(i) != nsubs {263 panic("wrong number of sends")264 }265 }266 b.StopTimer()267 done.Wait()268}...

Full Screen

Full Screen

encode_test.go

Source:encode_test.go Github

copy

Full Screen

1// SPDX-FileCopyrightText: 2021 The Go-SSB Authors2//3// SPDX-License-Identifier: MIT4package legacy5import (6 "archive/zip"7 "encoding/json"8 "errors"9 "fmt"10 "io/ioutil"11 "log"12 "testing"13 "github.com/kylelemons/godebug/diff"14 refs "go.mindeco.de/ssb-refs"15)16type testMessage struct {17 Author refs.FeedRef18 Hash, Signature string19 Input, NoSig []byte20}21var testMessages []testMessage22func init() {23 r, err := zip.OpenReader("testdata.zip")24 if err != nil {25 fmt.Println("could not find testdata - run 'node encode_test.js' to create it")26 checkPanic(err)27 }28 defer r.Close()29 if len(r.File)%3 != 0 {30 checkPanic(errors.New("expecting three files per message"))31 }32 testMessages = make([]testMessage, len(r.File)/3+1)33 seq := 134 for i := 0; i < len(r.File); i += 3 {35 full := r.File[i]36 input := r.File[i+1]37 noSig := r.File[i+2]38 // check file structure assumption39 if noSig.Name != fmt.Sprintf("%05d.noSig", seq) {40 checkPanic(fmt.Errorf("unexpected file. wanted '%05d.noSig' got %s", seq, noSig.Name))41 }42 if input.Name != fmt.Sprintf("%05d.input", seq) {43 checkPanic(fmt.Errorf("unexpected file. wanted '%05d.input' got %s", seq, input.Name))44 }45 if full.Name != fmt.Sprintf("%05d.full", seq) {46 checkPanic(fmt.Errorf("unexpected file. wanted '%05d.full' got %s", seq, full.Name))47 }48 // get some data from the full message49 var origMsg struct {50 Key string51 Value map[string]interface{}52 }53 origRC, err := full.Open()54 if err != nil {55 err = fmt.Errorf("test(%d) - failed to open full: %w", i, err)56 checkPanic(err)57 }58 err = json.NewDecoder(origRC).Decode(&origMsg)59 if err != nil {60 err = fmt.Errorf("test(%d) - could not json decode full: %w", i, err)61 checkPanic(err)62 }63 testMessages[seq].Hash = origMsg.Key64 // get sig65 sig, has := origMsg.Value["signature"]66 if !has {67 if err != nil {68 err = fmt.Errorf("test(%d) - expected signature in value field", i)69 checkPanic(err)70 }71 }72 testMessages[seq].Signature = sig.(string)73 // get author74 a, has := origMsg.Value["author"]75 if !has {76 if err != nil {77 err = fmt.Errorf("test(%d) - expected author in value field", i)78 checkPanic(err)79 }80 }81 testMessages[seq].Author, err = refs.ParseFeedRef(a.(string))82 if err != nil {83 err = fmt.Errorf("test(%d) - failed to parse author ref: %w", i, err)84 checkPanic(err)85 }86 // copy input87 rc, err := input.Open()88 if err != nil {89 err = fmt.Errorf("test(%d) - could not open wanted data: %w", i, err)90 checkPanic(err)91 }92 testMessages[seq].Input, err = ioutil.ReadAll(rc)93 if err != nil {94 err = fmt.Errorf("test(%d) - could not read all data: %w", i, err)95 checkPanic(err)96 }97 if err = rc.Close(); err != nil {98 err = fmt.Errorf("test(%d) - could not close input reader: %w", i, err)99 checkPanic(err)100 }101 // copy wanted output102 rc, err = noSig.Open()103 if err != nil {104 err = fmt.Errorf("test(%d) - could not open wanted data: %w", i, err)105 checkPanic(err)106 }107 testMessages[seq].NoSig, err = ioutil.ReadAll(rc)108 if err != nil {109 err = fmt.Errorf("test(%d) - could not read all wanted data: %w", i, err)110 checkPanic(err)111 }112 // cleanup113 if err = origRC.Close(); err != nil {114 err = fmt.Errorf("test(%d) - could not close reader #2: %w", i, err)115 checkPanic(err)116 }117 seq++118 }119 log.Printf("loaded %d messages from testdata.zip", seq)120}121func checkPanic(err error) {122 if err != nil {123 panic(err)124 }125}126func TestPreserveOrder(t *testing.T) {127 for i := 1; i < 20; i++ {128 tPresve(t, i)129 }130}131func tPresve(t *testing.T, i int) []byte {132 encoded, err := PrettyPrint(testMessages[i].Input)133 if err != nil {134 t.Errorf("PrettyPrint(%d) failed:\n%+v", i, err)135 }136 return encoded137}138func TestComparePreserve(t *testing.T) {139 n := len(testMessages)140 if testing.Short() {141 n = min(50, n)142 }143 for i := 1; i < n; i++ {144 w := string(testMessages[i].Input)145 pBytes := tPresve(t, i)146 p := string(pBytes)147 if d := diff.Diff(w, p); len(d) != 0 && t.Failed() {148 t.Logf("Seq:%d\n%s", i, d)149 }150 }151}...

Full Screen

Full Screen

strmap_test.go

Source:strmap_test.go Github

copy

Full Screen

1package strmap2import (3 // "errors"4 "reflect"5 "testing"6)7var sm = StrMap(map[string]interface{}{8 `key1`: `value1`,9 `key2`: map[interface{}]interface{}{`key`: `value`},10 `key3`: []interface{}{`value1`, `value2`, `value3`},11})12func checkPanic(t *testing.T, expect interface{}) {13 got := recover()14 if got == nil || got == expect || reflect.DeepEqual(got, expect) {15 return16 }17 t.Fatalf("unexpected Panic: %T(%#v)", got, got)18}19func TestGet1(t *testing.T) {20 defer checkPanic(t, "no key: nonexist")21 got := sm.Get(`nonexist`)22 t.Errorf("expect panic got: %v\n", got)23}24func TestGet2(t *testing.T) {25 defer checkPanic(t, `invalid character 'v' looking for beginning of value`)26 got := sm.Get(`key1`)27 t.Errorf("expect panic got: %v\n", got)28}29func TestGet3(t *testing.T) {30 defer checkPanic(t, "no key: nonexist")31 got := sm.Get(`key2`).Get(`nonexist`)32 t.Errorf("expect panic got: %v\n", got)33}34func TestGet4(t *testing.T) {35 defer checkPanic(t, `unable to cast []interface {}{"value1", "value2", "value3"}`+36 ` of type []interface {} to map[string]interface{}`)37 got := sm.Get(`key3`)38 t.Errorf("expect panic got: %v\n", got)39}40func TestGet5(t *testing.T) {41 expect := StrMap(map[string]interface{}{`key`: `value`})42 if got := sm.Get(`key2`); !reflect.DeepEqual(got, expect) {43 t.Errorf("expect: %v got: %v\n", expect, got)44 }45}46func TestGetString1(t *testing.T) {47 defer checkPanic(t, "no key: nonexist")48 got := sm.GetString(`nonexist`)49 t.Errorf("expect panic got: %v\n", got)50}51func TestGetString2(t *testing.T) {52 if expect, got := `value1`, sm.GetString(`key1`); got != expect {53 t.Errorf("expect: %v got: %v\n", expect, got)54 }55}56func TestGetString3(t *testing.T) {57 defer checkPanic(t, `unable to cast map[interface {}]interface {}{"key":"value"}`+58 ` of type map[interface {}]interface {} to string`)59 got := sm.GetString(`key2`)60 t.Errorf("expect panic got: %v\n", got)61}62func TestGetString4(t *testing.T) {63 defer checkPanic(t, `unable to cast []interface {}{"value1", "value2", "value3"}`+64 ` of type []interface {} to string`)65 got := sm.GetString(`key3`)66 t.Errorf("expect: panic got: %v\n", got)67}68func TestGetStringSlice1(t *testing.T) {69 defer checkPanic(t, "no key: nonexist")70 got := sm.GetStringSlice(`nonexist`)71 t.Errorf("expect panic got: %v\n", got)72}73func TestGetStringSlice2(t *testing.T) {74 if expect, got := []string{`value1`}, sm.GetStringSlice(`key1`); !reflect.DeepEqual(got, expect) {75 t.Errorf("expect: %v got: %v\n", expect, got)76 }77}78func TestGetStringSlice3(t *testing.T) {79 defer checkPanic(t, `unable to cast map[interface {}]interface {}{"key":"value"}`+80 ` of type map[interface {}]interface {} to []string`)81 got := sm.GetStringSlice(`key2`)82 t.Errorf("expect panic got: %v\n", got)83}84func TestGetStringSlice4(t *testing.T) {85 expect, got := []string{`value1`, `value2`, `value3`}, sm.GetStringSlice(`key3`)86 if !reflect.DeepEqual(got, expect) {87 t.Errorf("expect: %v got: %v\n", expect, got)88 }89}...

Full Screen

Full Screen

CheckPanic

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("main start")4 test.CheckPanic()5 fmt.Println("main end")6}

Full Screen

Full Screen

CheckPanic

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Inside main")4 test.CheckPanic()5 fmt.Println("End of main")6}

Full Screen

Full Screen

CheckPanic

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Calling test.CheckPanic()...")4 test.CheckPanic()5 fmt.Println("Returned normally from test.CheckPanic()")6}7import (8func main() {9 fmt.Println("Calling test.CheckPanic()...")10 test.CheckPanic()11 fmt.Println("Returned normally from test.CheckPanic()")12}

Full Screen

Full Screen

CheckPanic

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 test.CheckPanic()4 fmt.Println("After panic")5}6import (7func main() {8 test.CheckDefer()9 fmt.Println("After defer")10}11import (12func main() {13 test.CheckDeferPanic()14 fmt.Println("After defer")15}16import (17func main() {18 test.CheckDeferPanicRecover()19 fmt.Println("After defer")20}21import (22func main() {23 test.CheckDeferPanicRecover2()24 fmt.Println("After defer")25}26import (27func main() {28 test.CheckDeferPanicRecover3()29 fmt.Println("After defer")30}31import (32func main() {33 test.CheckDeferPanicRecover4()34 fmt.Println("After defer")35}

Full Screen

Full Screen

CheckPanic

Using AI Code Generation

copy

Full Screen

1func TestCheckPanic(t *testing.T) {2 test.CheckPanic(t, func() {3 })4}5func TestCheckPanic(t *testing.T) {6 test.CheckPanic(t, func() {7 })8}9func TestCheckPanic(t *testing.T) {10 test.CheckPanic(t, func() {11 })12}13func TestCheckPanic(t *testing.T) {14 test.CheckPanic(t, func() {15 })16}17func TestCheckPanic(t *testing.T) {18 test.CheckPanic(t, func() {19 })20}21func TestCheckPanic(t *testing.T) {22 test.CheckPanic(t, func() {23 })24}25func TestCheckPanic(t *testing.T) {26 test.CheckPanic(t, func() {27 })28}29func TestCheckPanic(t *testing.T) {30 test.CheckPanic(t, func() {31 })32}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful