Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

renanbastos93
renanbastos93

Posted on

     

How to verify if the context has a deadline

It's very simple,context.Context has a little method called Deadline that returns(deadline time.Time, ok bool). When the ok variable is true, the deadline is set. Why use a deadline? We need to use it to set a time to cancel the operation, e.g. we need to wait for max 3 seconds to execute a query on the database, in case overtakes the time, context will call cancel in the operation.

See the example below:

funcsomething(ctxcontext.Context)(context.Context,context.CancelFunc){if_,hasDeadline:=ctx.Deadline();!hasDeadline{returncontext.WithTimeout(ctx,time.Minute)}returncontext.WithCancel(ctx)}
Enter fullscreen modeExit fullscreen mode

Now, let's see an example using this method in an operation that prints a message many times.

funcusingSomething(){ctx,cancel:=something(context.Background())defercancel()for{select{case<-ctx.Done():returndefault:fmt.Println("something message!")time.Sleep(time.Second)}}}
Enter fullscreen modeExit fullscreen mode

Well, I believe that it’s interesting to share the unit test as well.

funcTestSomethingWithDeadline(t*testing.T){t.Run("using timeout of 1s",func(t*testing.T){ctx,_:=context.WithTimeout(context.Background(),time.Second)ctx,cancel:=something(ctx)defercancel()_,hasDeadline:=ctx.Deadline()assert.EqualValues(t,true,hasDeadline)})}funcTestSomethingWithoutDeadline(t*testing.T){t.Run("using default timeout of something method",func(t*testing.T){ctx,cancel:=something(context.Background())defercancel()_,hasDeadline:=ctx.Deadline()assert.EqualValues(t,true,hasDeadline)})}
Enter fullscreen modeExit fullscreen mode

That's what I wanted to show here. I hope this helps!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
dougds profile image
Douglas Oliveira
  • Work
    QA Analyst
  • Joined

Nice and easy! Ty

CollapseExpand
 
renanbastos93 profile image
renanbastos93
I love technology, innovation, and entrepreneurship. I have been working as a Software Engineer, but I'm always seeking to learn new things.
  • Email
  • Location
    Florianópolis - SC - Brazil
  • Education
    Analysis and systems development
  • Work
    Software Engineer
  • Joined

Thanks dude

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I love technology, innovation, and entrepreneurship. I have been working as a Software Engineer, but I'm always seeking to learn new things.
  • Location
    Florianópolis - SC - Brazil
  • Education
    Analysis and systems development
  • Work
    Software Engineer
  • Joined

More fromrenanbastos93

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp