|
| 1 | +// Copyright 2024 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package chainsync |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestHandleRequestNext_Callback(t *testing.T) { |
| 24 | + called := false |
| 25 | + server := &Server{ |
| 26 | + config: &Config{ |
| 27 | + RequestNextFunc: func(ctx CallbackContext) error { |
| 28 | + called = true |
| 29 | + return nil |
| 30 | + }, |
| 31 | + }, |
| 32 | + callbackContext: CallbackContext{}, |
| 33 | + } |
| 34 | + |
| 35 | + err := server.handleRequestNext() |
| 36 | + |
| 37 | + assert.NoError(t, err, "expected no error") |
| 38 | + assert.True(t, called, "expected RequestNextFunc to be called") |
| 39 | +} |
| 40 | + |
| 41 | +func TestHandleRequestNext_NilCallback(t *testing.T) { |
| 42 | + server := &Server{ |
| 43 | + config: &Config{ |
| 44 | + RequestNextFunc: nil, |
| 45 | + }, |
| 46 | + callbackContext: CallbackContext{}, |
| 47 | + } |
| 48 | + |
| 49 | + err := server.handleRequestNext() |
| 50 | + expectedError := "received chain-sync RequestNext message but no callback function is defined" |
| 51 | + |
| 52 | + assert.Error(t, err, "expected an error due to nil callback") |
| 53 | + assert.EqualError(t, err, expectedError) |
| 54 | +} |
0 commit comments