Skip to content

Proposal: Channel assertions #1859

@tallclair

Description

@tallclair

I've found the following test helpers useful (especially when paired with testing/synctest):

func requireBlocked[T any](t *testing.T, ch <-chan T) {
	t.Helper()
	select {
	case r := <-ch:
		t.Fatalf("Channel receive should have blocked, but got: %v", r)
	default:
		// OK.
	}
}

func requireUnblocked[T any](t *testing.T, ch <-chan T) (r T) {
	t.Helper()
	select {
	case r = <-ch:
	default:
		t.Fatalf("Channel receive should not have been blocked")
	}
	return
}

These seem like they could be useful assertions to include.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions